Skip to main content

Website

The cloud.Website resource represents a static website that can be hosted in the cloud. Websites are typically used to serve static content, such as HTML, CSS, and JavaScript files, which are updated whenever the application is redeployed.

Usage

Website

bring cloud;

let website = new cloud.Website(path: "./public");

Under ./public/index.html

<!DOCTYPE html>
<html>
Hello Winglang!!!
</html>

Webapp

An extended Web App example including static Website, API Gateway and a Redis database, can be found in this example project.

Target-specific details

Review the Website RFC for detailed information.

Pass variables to the website

You can pass dynamic variables from the main.w file to the website, and recuperate them by fetching the config.js file. // inside main.w

bring cloud;

let website = new cloud.Website(path: "./static");
let api = new cloud.Api();

website.addJson("config.json", { api: api.url });

inside ./static/index.html

<html lang="en">
<html>
<body>
...
<script>
// Fetch the config file and get the API URL
let ApiUrl;
fetch('/config.json')
.then(response => response.json())
.then(data => {
ApiUrl = data.api;
});
</script>
</body>
</html>

Simulator (sim)

sim implementations of cloud.Website is using nodejs express.

AWS (tf-aws and awscdk)

AWS implementations of cloud.Website uses Amazon S3 & Amazon CloudFront.

Azure (tf-azure)

🚧 Not supported yet (tracking issue: #1295)

GCP (tf-gcp)

🚧 Not supported yet (tracking issue: #1296)

API Reference

Website

A cloud static website.

Initializers

bring cloud;

new cloud.Website(props: WebsiteProps);
NameTypeDescription
props
WebsiteProps
No description.

propsRequired

Methods

Preflight Methods
NameDescription
addFile
Add a file to the website during deployment.
addJson
Add a JSON file with custom values during the website's deployment.

addFile
addFile(path: str, data: str, options?: AddFileOptions): str

Add a file to the website during deployment.

If the path conflicts with file path from the website's static assets, an error will be thrown.

pathRequired
  • Type: str

the file path it will be uploaded as.


dataRequired
  • Type: str

the data to write to the file.


optionsOptional

configure the file's options.


addJson
addJson(path: str, data: Json): str

Add a JSON file with custom values during the website's deployment.

If the path conflicts with file path from the website's static assets, an error will be thrown.

pathRequired
  • Type: str

the file path it will be uploaded as.


dataRequired

the data to write to the file.


Static Functions

NameDescription
onLiftType
A hook called by the Wing compiler once for each inflight host that needs to use this type inflight.
toInflight
Generates an asynchronous JavaScript statement which can be used to create an inflight client for a resource.

onLiftType
bring cloud;

cloud.Website.onLiftType(host: IInflightHost, ops: MutArray<str>);

A hook called by the Wing compiler once for each inflight host that needs to use this type inflight.

The list of requested inflight methods needed by the inflight host are given by ops.

This method is commonly used for adding permissions, environment variables, or other capabilities to the inflight host.

hostRequired

opsRequired
  • Type: MutArray<str>

toInflight
bring cloud;

cloud.Website.toInflight(obj: IResource);

Generates an asynchronous JavaScript statement which can be used to create an inflight client for a resource.

NOTE: This statement must be executed within an async context.

objRequired

Properties

NameTypeDescription
node
constructs.NodeThe tree node.
path
strAbsolute local path to the website's static files.
url
strThe website's url.

nodeRequired
node: Node;
  • Type: constructs.Node

The tree node.


pathRequired
path: str;
  • Type: str

Absolute local path to the website's static files.


urlRequired
url: str;
  • Type: str

The website's url.


Structs

AddFileOptions

Options for adding a file with custom value during the website's deployment.

Initializer

bring cloud;

let AddFileOptions = cloud.AddFileOptions{ ... };

Properties

NameTypeDescription
contentType
strFile's content type.

contentTypeOptional
contentType: str;
  • Type: str

File's content type.


WebsiteDomainOptions

Options for Website.

Initializer

bring cloud;

let WebsiteDomainOptions = cloud.WebsiteDomainOptions{ ... };

Properties

NameTypeDescription
domain
Domain
The website's custom domain object.

domainOptional
domain: Domain;
  • Type: Domain
  • Default: undefined

The website's custom domain object.


WebsiteOptions

Basic options for Website.

Initializer

bring cloud;

let WebsiteOptions = cloud.WebsiteOptions{ ... };

Properties

NameTypeDescription
path
strLocal path to the website's static files, relative to the Wing source file or absolute.
errorDocument
strName of the error document for the website.

pathRequired
path: str;
  • Type: str

Local path to the website's static files, relative to the Wing source file or absolute.


Example

"./dist"
errorDocumentOptional
errorDocument: str;
  • Type: str
  • Default: undefined

Name of the error document for the website.


Example

"404.html"

WebsiteProps

Options for Website.

Initializer

bring cloud;

let WebsiteProps = cloud.WebsiteProps{ ... };

Properties

NameTypeDescription
path
strLocal path to the website's static files, relative to the Wing source file or absolute.
errorDocument
strName of the error document for the website.
domain
Domain
The website's custom domain object.

pathRequired
path: str;
  • Type: str

Local path to the website's static files, relative to the Wing source file or absolute.


Example

"./dist"
errorDocumentOptional
errorDocument: str;
  • Type: str
  • Default: undefined

Name of the error document for the website.


Example

"404.html"
domainOptional
domain: Domain;
  • Type: Domain
  • Default: undefined

The website's custom domain object.


Protocols

IWebsite

Base interface for a website.

Properties

NameTypeDescription
url
strThe website URL.

urlRequired
url: str;
  • Type: str

The website URL.