@fastly/compute-js-static-publish 6.3.0 → 7.0.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +31 -586
- package/README.short.md +53 -0
- package/build/cli/commands/clean.d.ts +2 -0
- package/build/cli/commands/clean.d.ts.map +1 -0
- package/build/cli/commands/clean.js +121 -0
- package/build/cli/commands/collections/delete.d.ts +2 -0
- package/build/cli/commands/collections/delete.d.ts.map +1 -0
- package/build/cli/commands/collections/delete.js +88 -0
- package/build/cli/commands/collections/index.d.ts +2 -0
- package/build/cli/commands/collections/index.d.ts.map +1 -0
- package/build/cli/commands/collections/index.js +52 -0
- package/build/cli/commands/collections/list.d.ts +2 -0
- package/build/cli/commands/collections/list.d.ts.map +1 -0
- package/build/cli/commands/collections/list.js +62 -0
- package/build/cli/commands/index.d.ts +2 -0
- package/build/cli/commands/index.d.ts.map +1 -0
- package/build/cli/commands/index.js +67 -0
- package/build/cli/commands/init-app.d.ts +18 -2
- package/build/cli/commands/init-app.d.ts.map +1 -1
- package/build/cli/commands/init-app.js +299 -266
- package/build/cli/commands/publish-content.d.ts +2 -0
- package/build/cli/commands/publish-content.d.ts.map +1 -0
- package/build/cli/commands/publish-content.js +472 -0
- package/build/cli/compression/brotli.d.ts +1 -1
- package/build/cli/compression/brotli.d.ts.map +1 -1
- package/build/cli/compression/brotli.js +8 -11
- package/build/cli/compression/gzip.d.ts +1 -1
- package/build/cli/compression/gzip.d.ts.map +1 -1
- package/build/cli/compression/gzip.js +8 -11
- package/build/cli/compression/index.d.ts +3 -6
- package/build/cli/compression/index.d.ts.map +1 -1
- package/build/cli/compression/index.js +8 -9
- package/build/cli/fastly-api/api-token.d.ts +17 -0
- package/build/cli/fastly-api/api-token.d.ts.map +1 -0
- package/build/cli/fastly-api/api-token.js +94 -0
- package/build/cli/fastly-api/kv-store.d.ts +24 -0
- package/build/cli/fastly-api/kv-store.d.ts.map +1 -0
- package/build/cli/{util → fastly-api}/kv-store.js +42 -22
- package/build/cli/index.js +7 -91
- package/build/cli/util/args.d.ts +16 -0
- package/build/cli/util/args.d.ts.map +1 -0
- package/build/cli/util/args.js +55 -0
- package/build/cli/util/config.d.ts +13 -0
- package/build/cli/util/config.d.ts.map +1 -0
- package/build/cli/{load-config.js → util/config.js} +234 -187
- package/build/{util → cli/util}/content-types.d.ts +1 -2
- package/build/cli/util/content-types.d.ts.map +1 -0
- package/build/{util → cli/util}/content-types.js +11 -6
- package/build/cli/util/data.d.ts +3 -3
- package/build/cli/util/data.d.ts.map +1 -1
- package/build/cli/util/data.js +6 -2
- package/build/cli/util/files.d.ts +10 -2
- package/build/cli/util/files.d.ts.map +1 -1
- package/build/cli/util/files.js +53 -6
- package/build/cli/util/kv-store-items.d.ts +12 -0
- package/build/cli/util/kv-store-items.d.ts.map +1 -0
- package/build/cli/util/kv-store-items.js +161 -0
- package/build/cli/util/kv-store-local-server.d.ts +3 -0
- package/build/cli/util/kv-store-local-server.d.ts.map +1 -0
- package/build/cli/util/kv-store-local-server.js +35 -0
- package/build/cli/util/package.d.ts +9 -0
- package/build/cli/util/package.d.ts.map +1 -0
- package/build/cli/util/package.js +44 -0
- package/build/cli/util/retryable.d.ts +12 -0
- package/build/cli/util/retryable.d.ts.map +1 -0
- package/build/cli/util/retryable.js +43 -0
- package/build/cli/util/variants.d.ts +4 -0
- package/build/cli/util/variants.d.ts.map +1 -0
- package/build/cli/util/variants.js +27 -0
- package/build/index.d.ts +5 -5
- package/build/index.d.ts.map +1 -1
- package/build/index.js +7 -6
- package/build/models/assets/kvstore-assets.d.ts +17 -0
- package/build/models/assets/kvstore-assets.d.ts.map +1 -0
- package/build/models/assets/kvstore-assets.js +28 -0
- package/build/{constants/compression.d.ts → models/compression/index.d.ts} +2 -1
- package/build/models/compression/index.d.ts.map +1 -0
- package/build/models/compression/index.js +11 -0
- package/build/models/config/publish-content-config.d.ts +41 -0
- package/build/models/config/publish-content-config.d.ts.map +1 -0
- package/build/models/config/publish-content-config.js +5 -0
- package/build/models/config/publisher-server-config.d.ts +20 -0
- package/build/models/config/publisher-server-config.d.ts.map +1 -0
- package/build/models/config/publisher-server-config.js +5 -0
- package/build/models/config/static-publish-rc.d.ts +6 -0
- package/build/models/config/static-publish-rc.d.ts.map +1 -0
- package/build/models/config/static-publish-rc.js +5 -0
- package/build/server/collection-selector/from-config-store.d.ts +2 -0
- package/build/server/collection-selector/from-config-store.d.ts.map +1 -0
- package/build/server/collection-selector/from-config-store.js +15 -0
- package/build/server/collection-selector/from-cookie.d.ts +23 -0
- package/build/server/collection-selector/from-cookie.d.ts.map +1 -0
- package/build/server/collection-selector/from-cookie.js +98 -0
- package/build/server/collection-selector/from-request.d.ts +13 -0
- package/build/server/collection-selector/from-request.d.ts.map +1 -0
- package/build/server/collection-selector/from-request.js +22 -0
- package/build/server/collection-selector/index.d.ts +5 -0
- package/build/server/collection-selector/index.d.ts.map +1 -0
- package/build/server/collection-selector/index.js +7 -0
- package/build/server/collection-selector/request-collection-selector.d.ts +5 -0
- package/build/server/collection-selector/request-collection-selector.d.ts.map +1 -0
- package/build/server/collection-selector/request-collection-selector.js +5 -0
- package/build/server/publisher-server/index.d.ts +38 -0
- package/build/server/publisher-server/index.d.ts.map +1 -0
- package/build/server/publisher-server/index.js +418 -0
- package/build/server/publisher-server/serve-preconditions/if-modified-since.d.ts +3 -0
- package/build/server/publisher-server/serve-preconditions/if-modified-since.d.ts.map +1 -0
- package/build/server/{serve-preconditions → publisher-server/serve-preconditions}/if-modified-since.js +6 -2
- package/build/server/publisher-server/serve-preconditions/if-none-match.d.ts.map +1 -0
- package/build/server/{serve-preconditions → publisher-server/serve-preconditions}/if-none-match.js +4 -0
- package/build/server/util/cookies.d.ts +2 -0
- package/build/server/util/cookies.d.ts.map +1 -0
- package/build/server/util/cookies.js +14 -0
- package/build/server/util/kv-store.d.ts +3 -0
- package/build/server/util/kv-store.d.ts.map +1 -0
- package/build/server/util/kv-store.js +95 -0
- package/package.json +18 -10
- package/build/cli/commands/build-static.d.ts +0 -3
- package/build/cli/commands/build-static.d.ts.map +0 -1
- package/build/cli/commands/build-static.js +0 -541
- package/build/cli/commands/clean-kv-store.d.ts +0 -3
- package/build/cli/commands/clean-kv-store.d.ts.map +0 -1
- package/build/cli/commands/clean-kv-store.js +0 -55
- package/build/cli/load-config.d.ts +0 -6
- package/build/cli/load-config.d.ts.map +0 -1
- package/build/cli/presets/implementations/astro.d.ts +0 -7
- package/build/cli/presets/implementations/astro.d.ts.map +0 -1
- package/build/cli/presets/implementations/astro.js +0 -23
- package/build/cli/presets/implementations/create-react-app.d.ts +0 -11
- package/build/cli/presets/implementations/create-react-app.d.ts.map +0 -1
- package/build/cli/presets/implementations/create-react-app.js +0 -34
- package/build/cli/presets/implementations/docusaurus.d.ts +0 -7
- package/build/cli/presets/implementations/docusaurus.d.ts.map +0 -1
- package/build/cli/presets/implementations/docusaurus.js +0 -13
- package/build/cli/presets/implementations/gatsby.d.ts +0 -7
- package/build/cli/presets/implementations/gatsby.d.ts.map +0 -1
- package/build/cli/presets/implementations/gatsby.js +0 -13
- package/build/cli/presets/implementations/next.d.ts +0 -7
- package/build/cli/presets/implementations/next.d.ts.map +0 -1
- package/build/cli/presets/implementations/next.js +0 -13
- package/build/cli/presets/implementations/sveltekit.d.ts +0 -7
- package/build/cli/presets/implementations/sveltekit.d.ts.map +0 -1
- package/build/cli/presets/implementations/sveltekit.js +0 -13
- package/build/cli/presets/implementations/vite.d.ts +0 -7
- package/build/cli/presets/implementations/vite.d.ts.map +0 -1
- package/build/cli/presets/implementations/vite.js +0 -13
- package/build/cli/presets/implementations/vue.d.ts +0 -7
- package/build/cli/presets/implementations/vue.d.ts.map +0 -1
- package/build/cli/presets/implementations/vue.js +0 -24
- package/build/cli/presets/index.d.ts +0 -3
- package/build/cli/presets/index.d.ts.map +0 -1
- package/build/cli/presets/index.js +0 -20
- package/build/cli/presets/preset-base.d.ts +0 -21
- package/build/cli/presets/preset-base.d.ts.map +0 -1
- package/build/cli/presets/preset-base.js +0 -1
- package/build/cli/util/fastly-api.d.ts +0 -10
- package/build/cli/util/fastly-api.d.ts.map +0 -1
- package/build/cli/util/fastly-api.js +0 -47
- package/build/cli/util/hash.d.ts +0 -5
- package/build/cli/util/hash.d.ts.map +0 -1
- package/build/cli/util/hash.js +0 -9
- package/build/cli/util/id.d.ts +0 -3
- package/build/cli/util/id.d.ts.map +0 -1
- package/build/cli/util/id.js +0 -17
- package/build/cli/util/kv-store.d.ts +0 -16
- package/build/cli/util/kv-store.d.ts.map +0 -1
- package/build/cli/util/publish-id.d.ts +0 -5
- package/build/cli/util/publish-id.d.ts.map +0 -1
- package/build/cli/util/publish-id.js +0 -26
- package/build/compute-js.d.ts +0 -3
- package/build/compute-js.d.ts.map +0 -1
- package/build/compute-js.js +0 -2
- package/build/constants/compression.d.ts.map +0 -1
- package/build/constants/compression.js +0 -4
- package/build/server/assets/asset-manager.d.ts +0 -8
- package/build/server/assets/asset-manager.d.ts.map +0 -1
- package/build/server/assets/asset-manager.js +0 -14
- package/build/server/assets/content-asset-kv-store.d.ts +0 -19
- package/build/server/assets/content-asset-kv-store.d.ts.map +0 -1
- package/build/server/assets/content-asset-kv-store.js +0 -70
- package/build/server/assets/content-asset-wasm-inline.d.ts +0 -18
- package/build/server/assets/content-asset-wasm-inline.d.ts.map +0 -1
- package/build/server/assets/content-asset-wasm-inline.js +0 -52
- package/build/server/assets/content-assets.d.ts +0 -46
- package/build/server/assets/content-assets.d.ts.map +0 -1
- package/build/server/assets/content-assets.js +0 -100
- package/build/server/assets/module-assets.d.ts +0 -23
- package/build/server/assets/module-assets.d.ts.map +0 -1
- package/build/server/assets/module-assets.js +0 -45
- package/build/server/kv-store/inline-store-entry.d.ts +0 -25
- package/build/server/kv-store/inline-store-entry.d.ts.map +0 -1
- package/build/server/kv-store/inline-store-entry.js +0 -100
- package/build/server/publisher-server.d.ts +0 -23
- package/build/server/publisher-server.d.ts.map +0 -1
- package/build/server/publisher-server.js +0 -257
- package/build/server/serve-preconditions/if-modified-since.d.ts +0 -4
- package/build/server/serve-preconditions/if-modified-since.d.ts.map +0 -1
- package/build/server/serve-preconditions/if-none-match.d.ts.map +0 -1
- package/build/server/util/http.d.ts +0 -2
- package/build/server/util/http.d.ts.map +0 -1
- package/build/server/util/http.js +0 -9
- package/build/types/compute.d.ts +0 -11
- package/build/types/compute.d.ts.map +0 -1
- package/build/types/compute.js +0 -1
- package/build/types/config-normalized.d.ts +0 -34
- package/build/types/config-normalized.d.ts.map +0 -1
- package/build/types/config-normalized.js +0 -1
- package/build/types/config.d.ts +0 -37
- package/build/types/config.d.ts.map +0 -1
- package/build/types/config.js +0 -1
- package/build/types/content-assets.d.ts +0 -55
- package/build/types/content-assets.d.ts.map +0 -1
- package/build/types/content-assets.js +0 -1
- package/build/types/content-types.d.ts +0 -11
- package/build/types/content-types.d.ts.map +0 -1
- package/build/types/content-types.js +0 -1
- package/build/types/index.d.ts +0 -7
- package/build/types/index.d.ts.map +0 -1
- package/build/types/index.js +0 -1
- package/build/types/module-assets.d.ts +0 -15
- package/build/types/module-assets.d.ts.map +0 -1
- package/build/types/module-assets.js +0 -1
- package/build/types/util.d.ts +0 -7
- package/build/types/util.d.ts.map +0 -1
- package/build/types/util.js +0 -1
- package/build/util/content-types.d.ts.map +0 -1
- package/build/util/index.d.ts +0 -3
- package/build/util/index.d.ts.map +0 -1
- package/build/util/index.js +0 -2
- package/build/util/metadata.d.ts +0 -3
- package/build/util/metadata.d.ts.map +0 -1
- package/build/util/metadata.js +0 -12
- package/resources/README.md +0 -3
- package/resources/statics-metadata.d.ts +0 -10
- package/resources/statics.d.ts +0 -19
- package/resources/webpack.config.js +0 -41
- /package/build/server/{serve-preconditions → publisher-server/serve-preconditions}/if-none-match.d.ts +0 -0
package/LICENSE
CHANGED
package/README.md
CHANGED
|
@@ -1,608 +1,53 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @fastly/compute-js-static-publish
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Fastly Compute + KV Store for static websites and web apps.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
This CLI tool helps you:
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
- ✅ Deploy static sites to Fastly Compute with zero backend
|
|
8
|
+
- 📦 Store files in Fastly KV Store efficiently
|
|
9
|
+
- 🗂 Publish to named collections (`live`, `preview-42`, etc.)
|
|
10
|
+
- 🔄 Switch between collections at runtime
|
|
11
|
+
- 🧹 Clean up old or expired assets
|
|
8
12
|
|
|
9
|
-
|
|
13
|
+
---
|
|
10
14
|
|
|
11
|
-
|
|
15
|
+
## Quick Start
|
|
12
16
|
|
|
13
|
-
|
|
17
|
+
Create a directory for your project, place your static files in `./public`, then type:
|
|
14
18
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
```shell
|
|
18
|
-
npx @fastly/compute-js-static-publish@latest --root-dir=./public
|
|
19
|
+
```sh
|
|
20
|
+
npx @fastly/compute-js-static-publish --root-dir=./public --kv-store-name=site-content
|
|
19
21
|
```
|
|
20
22
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
> [!TIP]
|
|
24
|
-
> This process creates a `./compute-js/static-publish.rc.js` to hold your configuration. This, as well as the other files created in your new Compute program at `./compute-js`, can be committed to source control (except for the ones we specify in `.gitignore`!)
|
|
25
|
-
|
|
26
|
-
> [!IMPORTANT]
|
|
27
|
-
> This step generates an application that includes your files and a program that serves them, and needs to be run only once. To make modifications to your application, simply make changes to your static files and rebuild it. Read the rest of this section for more details.
|
|
23
|
+
### 🔧 Local Preview
|
|
28
24
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
The `start` script builds and runs your application using [Fastly's local development server](https://developer.fastly.com/learning/compute/testing/#running-a-local-testing-server).
|
|
32
|
-
|
|
33
|
-
```shell
|
|
34
|
-
cd ./compute-js
|
|
25
|
+
```sh
|
|
26
|
+
cd compute-js
|
|
35
27
|
npm install
|
|
36
|
-
npm run start
|
|
28
|
+
npm run start # preview locally
|
|
37
29
|
```
|
|
38
30
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
Once your application is running, your files will be served under `http://127.0.0.1:7676/` at the corresponding paths relative to the `./public` directory. For example, making a request to `http://127.0.0.1:7676/foo/bar.html` will attempt to serve the file at `./public/foo/bar.html`.
|
|
31
|
+
Serves your app at `http://127.0.0.1:7676`, powered by a simulated KV Store.
|
|
42
32
|
|
|
43
|
-
###
|
|
33
|
+
### 🚀 Deploy to Production
|
|
44
34
|
|
|
45
|
-
|
|
35
|
+
When you're ready to go live, [create a free Fastly account](https://www.fastly.com/signup/?tier=free) if you haven't already, and then:
|
|
46
36
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
### 4. When you're ready to go live, deploy your Compute service
|
|
53
|
-
|
|
54
|
-
The `deploy` script builds and [publishes your application to a Compute service in your Fastly account](https://developer.fastly.com/reference/cli/compute/publish/).
|
|
55
|
-
|
|
56
|
-
```shell
|
|
57
|
-
npm run deploy
|
|
37
|
+
```sh
|
|
38
|
+
cd compute-js
|
|
39
|
+
npm run publish-service # deploy the app (publish the "service")
|
|
40
|
+
npm run publish-content # upload your static files (publish the "content")
|
|
58
41
|
```
|
|
59
42
|
|
|
60
43
|
## Features
|
|
61
44
|
|
|
62
|
-
-
|
|
63
|
-
-
|
|
64
|
-
-
|
|
65
|
-
-
|
|
66
|
-
- Support for `If-None-Match` and `If-Modified-Since` request headers.
|
|
67
|
-
- Optionally use webpack as a module bundler.
|
|
68
|
-
- Selectively serve files from Fastly's [KV Store](#kv-store), or embedded into your Wasm module.
|
|
69
|
-
- Supports loading JavaScript files as code into your Compute application.
|
|
70
|
-
- Presets for several static site generators.
|
|
71
|
-
|
|
72
|
-
Some of these features are new! If you wish to update to this version, you may need to re-scaffold your application, or follow the steps outlined in [MIGRATING.md](./MIGRATING.md).
|
|
73
|
-
|
|
74
|
-
## How does it work? Where are the files?
|
|
75
|
-
|
|
76
|
-
Once your application is scaffolded, `@fastly/compute-js-static-publish` integrates into your development process by
|
|
77
|
-
running as part of your build process.
|
|
78
|
-
|
|
79
|
-
The files you have configured to be included (`--root-dir`) are enumerated and prepared. Their contents are included into
|
|
80
|
-
your Wasm binary (or made available via [KV Store](#kv-store), if so configured). This process is called "publishing".
|
|
81
|
-
|
|
82
|
-
Once the files are published, they are available to the other source files in the Compute application. For example,
|
|
83
|
-
the stock application runs the [PublisherServer](#publisherserver) class to serve these files.
|
|
84
|
-
|
|
85
|
-
For more advanced uses, such as accessing the contents of these file in your application, see the
|
|
86
|
-
[Using the packaged objects in your own application](#using-published-assets-in-your-own-application) section below.
|
|
87
|
-
|
|
88
|
-
Publishing is meant to run each time before building your Compute application into a Wasm file.
|
|
89
|
-
If the files in `--root-dir` have changed, then a new set of files will be published.
|
|
90
|
-
|
|
91
|
-
### Content Compression
|
|
92
|
-
|
|
93
|
-
During publishing, this tool supports pre-compression of content. By default, your assets are compressed using the Brotli
|
|
94
|
-
and gzip algorithms, and then stored alongside the original files in your Wasm binary (or [KV Store](#kv-store)).
|
|
95
|
-
|
|
96
|
-
> [!IMPORTANT]
|
|
97
|
-
> By default, pre-compressed content assets are not generated when the KV Store is not used.
|
|
98
|
-
This is done to prevent the inclusion multiple of copies of each asset from making the Wasm binary too large.
|
|
99
|
-
If you want to pre-compress assets when not using KV Store, add a value for 'contentCompression' to your
|
|
100
|
-
`static-publish.rc.js` file.
|
|
101
|
-
|
|
102
|
-
## CLI options
|
|
103
|
-
|
|
104
|
-
Except for `--root-dir`, most arguments are optional.
|
|
105
|
-
|
|
106
|
-
```shell
|
|
107
|
-
npx @fastly/compute-js-static-publish \
|
|
108
|
-
--root-dir=./build \
|
|
109
|
-
--public-dir=./build/public \
|
|
110
|
-
--static-dir=./build/public/static \
|
|
111
|
-
--output=./compute-js \
|
|
112
|
-
--spa=./build/spa.html
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
If you provide options, they override the defaults described below.
|
|
116
|
-
|
|
117
|
-
Any configuration options will be written to a `static-publish.rc.js` file, and used each time you build your Compute
|
|
118
|
-
application.
|
|
119
|
-
|
|
120
|
-
On subsequent builds of your Compute application, `compute-js-static-publish` will run with a special flag, `build-static`,
|
|
121
|
-
reading from stored configuration, then scanning the `--public-dir` directory to recreate `./compute-js/static-publisher/statics.js`.
|
|
122
|
-
|
|
123
|
-
Any relative file and directory paths passed at the command line are handled as relative to the current directory.
|
|
124
|
-
|
|
125
|
-
### Publishing options:
|
|
126
|
-
|
|
127
|
-
| Option | Default | Description |
|
|
128
|
-
|-----------------------------|------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
129
|
-
| `--preset` | (None) | Apply default options from a specified preset. See ["Frameworks and Static Site Generators"](#usage-with-frameworks-and-static-site-generators). |
|
|
130
|
-
| `--output` | `./compute-js` | The directory in which to create the Compute application. |
|
|
131
|
-
| `--static-content-root-dir` | (output directory) + `/static-publisher` | The directory under the Compute application where static asset and metadata are written. |
|
|
132
|
-
| `--root-dir` | (None) | **Required**. The root of the directory that contains the files to include in the publishing. All files you wish to include must reside under this root. |
|
|
133
|
-
|
|
134
|
-
### Server options:
|
|
135
|
-
|
|
136
|
-
Used to populate the `server` key under `static-publish.rc.js`.
|
|
137
|
-
|
|
138
|
-
| Option | Default | Description |
|
|
139
|
-
|--------------------|-------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
140
|
-
| `--public-dir` | <root-dir> | The directory that contains your website's public files. |
|
|
141
|
-
| `--static-dir` | (None) | Any directories under `--public-dir` that contain the website's static assets that will be served with a very long TTL. You can specify as many such directories as you wish, by listing multiple items. |
|
|
142
|
-
| `--auto-ext` | `.html,.htm` | Specify automatic file extensions. |
|
|
143
|
-
| `--auto-index` | `index.html,index.htm` | Specify filenames for automatically serving an index file. |
|
|
144
|
-
| `--spa` | (None) | Path to a fallback file for SPA applications. |
|
|
145
|
-
| `--not-found-page` | `<public-dir>/404.html` | Path to a fallback file for 404 Not Found. |
|
|
146
|
-
|
|
147
|
-
See [PublisherServer](#publisherserver) for more information about these features.
|
|
148
|
-
|
|
149
|
-
For backwards compatibility, if you do not specify a `--root-dir` but you have provided a `--public-dir`, then that value is used for `--root-dir`.
|
|
150
|
-
|
|
151
|
-
Note that the files referenced by `--spa` and `--not-found-page` do not necessarily have to reside inside `--public-dir`.
|
|
152
|
-
|
|
153
|
-
### Fastly service options
|
|
154
|
-
|
|
155
|
-
These arguments are used to populate the `fastly.toml` and `package.json` files of your Compute application.
|
|
156
|
-
|
|
157
|
-
| Option | Default | Description |
|
|
158
|
-
|-------------------|--------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
159
|
-
| `--name` | `name` from `package.json`, or `compute-js-static-site` | The name of your Compute application. |
|
|
160
|
-
| `--description` | `description` from `package.json`, or `Compute static site` | The description of your Compute application. |
|
|
161
|
-
| `--author` | `author` from `package.json`, or `you@example.com` | The author of your Compute application. |
|
|
162
|
-
| `--service-id` | (None) | The ID of an existing Fastly WASM service for your Compute application. |
|
|
163
|
-
| `--kv-store-name` | (None) | The name of an existing [Fastly KV Store](https://developer.fastly.com/learning/concepts/data-stores/#kv-stores) to hold the content assets. In addition to already existing, it must be linked to the service specified by `--service-id`. |
|
|
164
|
-
|
|
165
|
-
## Usage with frameworks and static site generators
|
|
166
|
-
|
|
167
|
-
`compute-js-static-publish` supports preset defaults for a number of frameworks and static site generators:
|
|
168
|
-
|
|
169
|
-
| `--preset` | `--root-dir` | `--static-dir` | Notes |
|
|
170
|
-
|-------------------------------|--------------|------------------|-------------------------------------------------------------------------------------------------------------------------------------|
|
|
171
|
-
| `cra` (or `create-react-app`) | `./build` | `./build/static` | For apps written using [Create React App](https://create-react-app.dev). Checks for a dependency on `react-scripts`. |
|
|
172
|
-
| `cra-eject` | `./build` | `./build/static` | For apps written using Create React App, but which have since been ejected via `npm run eject`. Does not check for `react-scripts`. |
|
|
173
|
-
| `vite` | `./dist` | (None) | For apps written using [Vite](https://vitejs.dev). |
|
|
174
|
-
| `sveltekit` | `./dist` | (None) | For apps written using [SvelteKit](https://kit.svelte.dev). |
|
|
175
|
-
| `vue` | `./dist` | (None) | For apps written using [Vue](https://vuejs.org), and that were created using [create-vue](https://github.com/vuejs/create-vue). |
|
|
176
|
-
| `next` | `./out` | (None) | For apps written using [Next.js](https://nextjs.org), using `npm run export`. *1 |
|
|
177
|
-
| `astro` | `./dist` | (None) | For apps written using [Astro](https://astro.build) (static apps only). *2 |
|
|
178
|
-
| `gatsby` | `./public` | (None) | For apps written using [Gatsby](https://www.gatsbyjs.com). |
|
|
179
|
-
| `docusaurus` | `./build` | (None) | For apps written using [Docusaurus](https://docusaurus.io) |
|
|
180
|
-
|
|
181
|
-
You may still override any of these options individually.
|
|
182
|
-
|
|
183
|
-
*1 - For Next.js, consider using `@fastly/next-compute-js`, a Next.js server implementation that allows you to run
|
|
184
|
-
your Next.js application on Compute.
|
|
185
|
-
|
|
186
|
-
*2 - Astro support does not support SSR.
|
|
187
|
-
|
|
188
|
-
## PublisherServer
|
|
189
|
-
|
|
190
|
-
`PublisherServer` is a simple yet powerful server that can be used out of the box to serve the files prepared by this tool.
|
|
191
|
-
|
|
192
|
-
This server handles the following automatically:
|
|
193
|
-
|
|
194
|
-
* Maps the path of your request to a path under `--public-dir` and serves the content of the asset
|
|
195
|
-
* Sources the content from the content packaged in the Wasm binary, or from the [KV Store](#kv-store), if configured.
|
|
196
|
-
* Applies long-lived cache headers to files served from `--static-dir` directories. Files under these directories will be cached by the browser for 1 year. (Use versioned or hashed filenames to avoid serving stale assets.)
|
|
197
|
-
* Performs Brotli and gzip compression as requested by the `Accept-Encoding` headers.
|
|
198
|
-
* Provides `Last-Modified` and `ETag` response headers, and uses them with `If-Modified-Since` and `If-None-Match` request headers to produce `304 Not Modified` responses.
|
|
199
|
-
* If an exact match is not found for the request path, applies automatic extensions (e.g., `.html`) and automatic index files (e.g., `index.html`).
|
|
200
|
-
* Can be configured to serve a fallback file for SPA apps. Useful for apps that use [client-side routing](https://create-react-app.dev/docs/deployment#serving-apps-with-client-side-routing).
|
|
201
|
-
* Can be configured to serve a 404 not found file.
|
|
202
|
-
* Returns `null` if nothing matches, so that you can add your own handling if necessary.
|
|
203
|
-
|
|
204
|
-
During initial scaffolding, the configuration based on the command-line parameters and preset are written to your `./static-publisher.rc.js` file under the `server` key.
|
|
205
|
-
|
|
206
|
-
### Configuring PublisherServer
|
|
207
|
-
|
|
208
|
-
You can further configure the server by making modifications to the `server` key under `./static-publisher.rc.js`.
|
|
209
|
-
|
|
210
|
-
| Key | Default | Description |
|
|
211
|
-
|--------------------|--------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
212
|
-
| `publicDirPrefix` | `''` | Prefix to apply to web requests. Effectively, a directory within `rootDir` that is used by the web server to determine the asset to respond with. |
|
|
213
|
-
| `staticItems` | `[]` | A test to apply to item names to decide whether to serve them as "static" files, in other words, with a long TTL. These are used for files that are not expected to change. They can be provided as a string or array of strings. |
|
|
214
|
-
| `compression` | `[ 'br', 'gzip' ]` | If the request contains an `Accept-Encoding` header, they are checked for the values listed here. The compression algorithm that produces the smallest transfer size is applied. |
|
|
215
|
-
| `autoExt` | `[]` | When a file is not found, and it doesn't end in a slash, then try auto-ext: we try to serve a file with the same name post-fixed with the specified strings, tested in the order listed. These are tested before auto-index, if any. |
|
|
216
|
-
| `autoIndex` | `[]` | When a file is not found, then try auto-index: we treat it as a directory, then try to serve a file that has the specified strings, tested in the order listed. |
|
|
217
|
-
| `spaFile` | `null` | Asset key of a content item to serve with a status code of `200` when a GET request comes arrives for an unknown asset, and the Accept header includes text/html. |
|
|
218
|
-
| `notFoundPageFile` | `null` | Asset key of a content item to serve with a status code of `404` when a GET request comes arrives for an unknown asset, and the Accept header includes text/html. |
|
|
219
|
-
|
|
220
|
-
For `staticItems`:
|
|
221
|
-
* Items that contain asterisks are interpreted as glob patterns (for example, `/public/static/**/*.js`)
|
|
222
|
-
* Items that end with a trailing slash are interpreted as a directory name.
|
|
223
|
-
* Items that don't contain asterisks and that do not end in slash are checked for exact match.
|
|
224
|
-
|
|
225
|
-
For `compression`, the following values are allowed:
|
|
226
|
-
* `'br'` - Brotli
|
|
227
|
-
* `'gzip'` - Gzip
|
|
228
|
-
|
|
229
|
-
## Associating your project with a Fastly Service
|
|
230
|
-
|
|
231
|
-
The project created by this tool is a Fastly Compute JavaScript application, complete with a `fastly.toml` file that
|
|
232
|
-
describes your project to the Fastly CLI.
|
|
233
|
-
|
|
234
|
-
To deploy your project to production, you deploy it to a [Fastly service](https://developer.fastly.com/reference/glossary#term-service)
|
|
235
|
-
in your account. Usually, you create your service automatically as part of your first deployment of the project.
|
|
236
|
-
|
|
237
|
-
In this case, `fastly.toml` has no value for `service_id` at the time you deploy, so the Fastly CLI will prompt
|
|
238
|
-
you to create a Fastly service in your account, after which it will save the new service's ID to your `fastly.toml` file.
|
|
239
|
-
|
|
240
|
-
Alternatively, you may deploy to a service that already exists. You can create this service using the
|
|
241
|
-
[Fastly CLI](https://developer.fastly.com/reference/cli/service/create/) or the [Fastly web app](https://manage.fastly.com/).
|
|
242
|
-
Note that since this is a Compute application, the service must be created as a Wasm service.
|
|
243
|
-
|
|
244
|
-
Before deploying your application, specify the service by setting the `service_id` value in the `fastly.toml` file to the
|
|
245
|
-
ID of the service. The Fastly CLI will deploy to the service identified by this value.
|
|
246
|
-
|
|
247
|
-
To specify the service at the time you are scaffolding the project (for example, if you are running this tool and deploying
|
|
248
|
-
as part of a CI process), specify the `--service-id` command line argument to populate `fastly.toml` with this value.
|
|
249
|
-
|
|
250
|
-
## Using the KV Store (BETA)
|
|
251
|
-
|
|
252
|
-
<div id="kv-store"></div>
|
|
253
|
-
|
|
254
|
-
Starting with v4, it's now possible to upload assets to and serve them from a [Fastly KV Store](https://developer.fastly.com/learning/concepts/data-stores/#kv-stores).
|
|
255
|
-
|
|
256
|
-
When this mode is enabled, you build your application as normal, and as a step during the build, your files
|
|
257
|
-
are uploaded to the Fastly KV Store, and metadata in the application is marked to source them from there instead
|
|
258
|
-
of from bytes in the Wasm binary.
|
|
259
|
-
|
|
260
|
-
You can enable the use of KV Store with `@fastly/compute-js-static-publish` as you scaffold your application, or
|
|
261
|
-
at any later time.
|
|
262
|
-
|
|
263
|
-
At the time you enable the use of KV Store:
|
|
264
|
-
|
|
265
|
-
* Your Fastly service must already exist. See [Associating your project with a Fastly Service](#associating-your-project-with-a-fastly-service) above.
|
|
266
|
-
|
|
267
|
-
* Your KV Store must already exist under the same Fastly account, and be linked to the service.
|
|
268
|
-
As of this writing, to create the KV Store you will need to use either the Fastly CLI [fastly kv-store create](https://developer.fastly.com/reference/cli/kv-store/create/)
|
|
269
|
-
or the Fastly [KV Store API](https://developer.fastly.com/reference/api/services/resources/kv-store/#create-store).
|
|
270
|
-
Once the KV Store is created, you must link it to your Fastly service using the [Resource API](https://developer.fastly.com/reference/api/services/resource/#create-resource).
|
|
271
|
-
|
|
272
|
-
```shell
|
|
273
|
-
# Create a KV Store
|
|
274
|
-
$ curl -i -X POST "https://api.fastly.com/resources/stores/kv" -H "Fastly-Key: YOUR_FASTLY_TOKEN" -H "Content-Type: application/json" -H "Accept: application/json" -d '{"name":"example-store"}'
|
|
275
|
-
|
|
276
|
-
# Link the KV Store to a service
|
|
277
|
-
$ curl -i -X POST "https://api.fastly.com/service/YOUR_FASTLY_SERVICE_ID/version/YOUR_FASTLY_SERVICE_VERSION/resource" -H "Fastly-Key: YOUR_FASTLY_TOKEN" -H "Content-Type: application/x-www-form-urlencoded" -H "Accept: application/json" -d "name=example-store-service-a&resource_id=YOUR_KV_STORE_ID"
|
|
278
|
-
```
|
|
279
|
-
|
|
280
|
-
Once the KV Store is created and linked to your service, add its name to your `static-publish.rc.js`
|
|
281
|
-
file under the `kvStoreName` key.
|
|
282
|
-
|
|
283
|
-
To specify the KV Store at the time you are scaffolding the project (for example, if you are running this tool and
|
|
284
|
-
deploying as part of a CI process), specify the `--service-id` and `--kv-store-name` command line arguments to populate
|
|
285
|
-
the respective files with these values.
|
|
286
|
-
|
|
287
|
-
After you have performed the above steps, go ahead and build your application as normal.
|
|
288
|
-
As a new step during the build process, the tool will send these files to the KV Store.
|
|
289
|
-
|
|
290
|
-
> [!IMPORTANT]
|
|
291
|
-
> This step writes to your KV Store. When building your application, you must set the environment variable `FASTLY_API_TOKEN` to a Fastly API token that has access to write to this KV Store.
|
|
292
|
-
>
|
|
293
|
-
> Alternatively, if this environment variable is not found, the tool will attempt to detect an API token by calling `fastly profile token`.
|
|
294
|
-
|
|
295
|
-
> [!TIP]
|
|
296
|
-
> By running `npx @fastly/cli compute build --verbose` (or `npm run build` directly), you should see output in your logs saying that files are being sent to the KV Store.
|
|
297
|
-
|
|
298
|
-
The `statics-metadata.js` file should now show `"type": "kv-store"` for content assets.
|
|
299
|
-
Your Wasm binary should also be smaller, as the content of the files are no longer inlined in the build artifact.
|
|
300
|
-
You can deploy this and run it from Fastly, and the referenced files will be served from KV Store.
|
|
301
|
-
|
|
302
|
-
You will also see entries in `fastly.toml` that represent the local KV Store.
|
|
303
|
-
These enable the site to also run correctly when served using the local development environment.
|
|
304
|
-
|
|
305
|
-
### Cleaning unused items from KV Store
|
|
306
|
-
|
|
307
|
-
The files that are uploaded to the KV Store are submitted using keys of the following format:
|
|
308
|
-
|
|
309
|
-
`<publish-id>:<asset-path>_<alg>_<hash>`
|
|
310
|
-
|
|
311
|
-
For example:
|
|
312
|
-
`12345abcde67890ABCDE00:/public/index.html_br_aeed29478691e67f6d5s36b4ded20c17e9eae437614617067a8751882368b965`
|
|
313
|
-
|
|
314
|
-
Using such a key ensures that whenever the file contents are identical, the same key will be generated.
|
|
315
|
-
This enables to detect whether an unchanged file already exists in the KV Store, avoiding having to re-submit
|
|
316
|
-
files that have not changed. If the file contents have changed, then a new hash is generated. This ensures that
|
|
317
|
-
even during the brief amount of time between deploys, any request served by a prior version will still serve the same
|
|
318
|
-
corresponding previous version of the content.
|
|
319
|
-
|
|
320
|
-
However, this system never deletes files automatically. After many deployments, extraneous files may be left over.
|
|
321
|
-
|
|
322
|
-
`@fastly/compute-js-static-publish` includes a feature to delete these old versions of the files that are no longer being
|
|
323
|
-
used. To run it, type the following command:
|
|
324
|
-
|
|
325
|
-
`npx @fastly/compute-js-static-publish --clean-kv-store`
|
|
326
|
-
|
|
327
|
-
It works by scanning `statics-metadata.js` for all currently-used keys. Then it enumerates all the existing
|
|
328
|
-
keys in the configured KV Store and that belong to this application (can do so by narrowing down all keys to the ones
|
|
329
|
-
that begin with the "publish id"). If any of the keys is not in the list of currently-used keys, then a request is made
|
|
330
|
-
to delete that KV Store value.
|
|
331
|
-
|
|
332
|
-
And that's it! It should be possible to run this task to clean up once in a while.
|
|
333
|
-
|
|
334
|
-
## Advanced Usages
|
|
335
|
-
|
|
336
|
-
### The `static-publish.rc.js` config file <a name="static-publish-rc"></a>
|
|
337
|
-
|
|
338
|
-
* `rootDir` - All files under this root directory will be included by default in the publishing,
|
|
339
|
-
except for those that are excluded using some of the following features. Files outside this root cannot be
|
|
340
|
-
included in the publishing.
|
|
341
|
-
|
|
342
|
-
* `staticContentRootDir` - Static asset loader and metadata files are created under this directory.
|
|
343
|
-
For legacy compatibility, if not provided, defaults to `'./src'`.
|
|
344
|
-
|
|
345
|
-
* `kvStoreName` - Set this value to the _name_ of an existing KV Store to enable uploading of content assets
|
|
346
|
-
to Fastly KV Store. See [Using the KV Store](#kv-store) for more information.
|
|
347
|
-
|
|
348
|
-
* `excludeDirs` - Specifies names of files and directories within `rootDir` to exclude from the publishing. Each entry can
|
|
349
|
-
be a string or a JavaScript `RegExp` object. Every file and directory under `rootDir` is checked against each entry of
|
|
350
|
-
the array by testing its path relative to `rootDir`. The file or directory (included all children) and excluded if the
|
|
351
|
-
condition matches:
|
|
352
|
-
* If a string is specified, then an exact match is checked.
|
|
353
|
-
* If a `RegExp` is specified, then it is tested with the regular expression.
|
|
354
|
-
* If this setting is not set, then the default value is `['./node_modules']`.
|
|
355
|
-
* If you specifically set this to the empty array, then no files are excluded by this mechanism.
|
|
356
|
-
|
|
357
|
-
* `excludeDotfiles` - Unless disabled, will exclude all files and directories (and their children)
|
|
358
|
-
whose names begin with a `'.''`. This is `true` by default.
|
|
359
|
-
|
|
360
|
-
* `includeWellKnown` - Unless disabled, will include a file or directory called `.well-known`
|
|
361
|
-
even if `excludeDotfiles` would normally exclude it. This is `true` by default.
|
|
362
|
-
|
|
363
|
-
* `contentAssetInclusionTest` - Optionally specify a test function that can be run against each enumerated asset during
|
|
364
|
-
the publishing, to determine whether to include the asset as a content asset. For every file, this function is passed
|
|
365
|
-
the [asset key](#asset-keys), as well as its content type (MIME type string). You may return one of three values from
|
|
366
|
-
this function:
|
|
367
|
-
* Boolean `true` - Include the file as a content asset in this publishing. Upload the file to and serve it from the
|
|
368
|
-
KV Store if KV Store mode is enabled, or include the contents of the file in the Wasm binary if KV Store
|
|
369
|
-
mode is not enabled.
|
|
370
|
-
* String `"inline"` - Include the file as a content asset in this publishing. Include the contents of the file in the
|
|
371
|
-
Wasm binary, regardless of whether KV Store mode is enabled.
|
|
372
|
-
* Boolean `false` - Do not include this file as a content asset in this publishing.
|
|
373
|
-
|
|
374
|
-
If you do not provide a function, then every file will be included in this publishing as a content asset, and their
|
|
375
|
-
contents will be uploaded to and served from the KV Store if KV Store mode is enabled, or included in the Wasm
|
|
376
|
-
binary if KV Store mode is not enabled.
|
|
377
|
-
|
|
378
|
-
* `contentCompression` - During the publishing, the tool will pre-generate compressed versions of content assets in these
|
|
379
|
-
formats and make them available to the Publisher Server or your application. Default value is [ 'br' | 'gzip' ] if
|
|
380
|
-
KV Store is enabled, or [] if KV Store is not enabled.
|
|
381
|
-
|
|
382
|
-
* `moduleAssetInclusionTest` - Optionally specify a test function that can be run against each enumerated asset during
|
|
383
|
-
the publishing, to determine whether to include the asset as a module asset. For every file, this function is passed
|
|
384
|
-
the [asset key](#asset-keys), as well as its content type (MIME type string). You may return one of three values from this function:
|
|
385
|
-
* `true` (boolean) - Include the file as a module asset in this publishing.
|
|
386
|
-
* `"static-import"` (string) - Include the file as a module asset in this publishing, and statically import it. This causes
|
|
387
|
-
any top-level code in these modules to run at application initialization time.
|
|
388
|
-
* `false` (boolean) - Do not include this file as a module asset in this publishing.
|
|
389
|
-
|
|
390
|
-
If you do not provide a function, then no module assets will be included in this publishing.
|
|
391
|
-
|
|
392
|
-
* `contentTypes` - Provide custom content types and/or override them.
|
|
393
|
-
|
|
394
|
-
This tool comes with a [default set of content types](./src/util/content-types.ts) defined for many common
|
|
395
|
-
file extensions. This list can be used to add to and/or override items in the default list.
|
|
396
|
-
Content type definitions are checked in the provided order, and if none of them match, the default content types are
|
|
397
|
-
tested afterward.
|
|
398
|
-
|
|
399
|
-
Provide these as an array of content type definition objects, each with the following keys and values:
|
|
400
|
-
* `test` - a RegExp or function to perform on the asset key. If the test succeeds, then the content asset is considered
|
|
401
|
-
to be of this content type definition.
|
|
402
|
-
* `contentType` - The content type header to apply when serving an asset of this content type definition.
|
|
403
|
-
* `text` - If `true`, this content type definition is considered to contain textual data. This makes `.text()` and `.json()`
|
|
404
|
-
available for calling on store entries. If not specified, this is treated as `false`.
|
|
405
|
-
|
|
406
|
-
For example, to add a custom content type `application/x-custom` for files that have a `.custom` extension, and not treat
|
|
407
|
-
it as a text file, add the following to your `static-publish.rc.js` file:
|
|
408
|
-
|
|
409
|
-
```javascript
|
|
410
|
-
const config = {
|
|
411
|
-
/* ... other config ... */
|
|
412
|
-
contentTypes: [
|
|
413
|
-
{ test: /\.custom$/, contentType: 'application/x-custom', text: false },
|
|
414
|
-
],
|
|
415
|
-
};
|
|
416
|
-
```
|
|
417
|
-
|
|
418
|
-
> Note that content types are tested at publishing time, not at runtime.
|
|
419
|
-
|
|
420
|
-
* `server` - [Configuration of `PublisherServer()`](#configuring-publisherserver).
|
|
421
|
-
above.
|
|
422
|
-
|
|
423
|
-
### Running custom code alongside Publisher Server
|
|
424
|
-
|
|
425
|
-
The generated `./src/index.js` program instantiates the server and simply asks it to respond to a request.
|
|
426
|
-
|
|
427
|
-
You are free to add code to this file.
|
|
428
|
-
|
|
429
|
-
For example, if the `PublisherServer` is unable to formulate a response to the request, then it returns `null`. You may
|
|
430
|
-
add your own code to handle these cases, such as to provide custom responses.
|
|
431
|
-
|
|
432
|
-
```js
|
|
433
|
-
import { getServer } from './statics.js';
|
|
434
|
-
const staticContentServer = getServer();
|
|
435
|
-
|
|
436
|
-
addEventListener("fetch", (event) => event.respondWith(handleRequest(event)));
|
|
437
|
-
async function handleRequest(event) {
|
|
438
|
-
|
|
439
|
-
const response = await staticContentServer.serveRequest(event.request);
|
|
440
|
-
if (response != null) {
|
|
441
|
-
return response;
|
|
442
|
-
}
|
|
443
|
-
|
|
444
|
-
// Do custom things here!
|
|
445
|
-
// Handle API requests, serve non-static responses, etc.
|
|
446
|
-
|
|
447
|
-
return new Response('Not found', { status: 404 });
|
|
448
|
-
}
|
|
449
|
-
```
|
|
450
|
-
|
|
451
|
-
### Using published assets in your own application
|
|
452
|
-
|
|
453
|
-
Publishing, as described earlier, is the process of preparing files for inclusion into your application.
|
|
454
|
-
This process also makes metadata available about each of the files that are included, such as its content type, the last
|
|
455
|
-
modified date, the file hash, and so on.
|
|
456
|
-
|
|
457
|
-
The [`PublisherServer` class](#publisherserver) used by the default scaffolded application is a simple application of this content
|
|
458
|
-
and metadata. By importing `./statics.js` into your Compute application, you can just as easily access this
|
|
459
|
-
information about the assets that were included during publishing.
|
|
460
|
-
|
|
461
|
-
> IMPORTANT: Use a static `import` statement, rather than using `await import()` to load `./statics.js`, in order to
|
|
462
|
-
ensure that its top-level code runs during the initialization phase of your Compute application.
|
|
463
|
-
|
|
464
|
-
#### Assets
|
|
465
|
-
|
|
466
|
-
There are two categories of assets: Content Assets and Module Assets.
|
|
467
|
-
|
|
468
|
-
* A Content Asset is a type of asset where your application or a user of your application is interested in the text or
|
|
469
|
-
binary contents of an asset.
|
|
470
|
-
|
|
471
|
-
The data of each content asset can exist in one of two stores:
|
|
472
|
-
* Inline Store - this is a data store that exists within the Wasm binary.
|
|
473
|
-
* Fastly KV Store - Fastly's distributed edge data store. Data can be placed here without impacting the size of
|
|
474
|
-
your Wasm binary.
|
|
475
|
-
|
|
476
|
-
Your application can stream the contents of these assets to a visitor, or read from the stream itself and access its
|
|
477
|
-
contents.
|
|
478
|
-
|
|
479
|
-
* A Module Asset is a type of asset where your application wants to load the asset as a module, and use it as part of its
|
|
480
|
-
running code. Their contents are actually built at publishing time and their built representation is included in the Wasm
|
|
481
|
-
binary. They can be imported statically at will, and your application is able to execute the code exported by these modules.
|
|
482
|
-
|
|
483
|
-
##### Asset Keys
|
|
484
|
-
|
|
485
|
-
When working with content assets or module assets from your application, they are referenced by their asset key, which
|
|
486
|
-
is the relative path of the file from `rootDir`, including the leading slash.
|
|
487
|
-
|
|
488
|
-
#### Content Assets
|
|
489
|
-
|
|
490
|
-
You can obtain the content assets included in publishing by importing the `contentAssets` object exported from
|
|
491
|
-
`./statics.js`.
|
|
492
|
-
|
|
493
|
-
```js
|
|
494
|
-
import { contentAssets } from './statics';
|
|
495
|
-
|
|
496
|
-
// Obtain a content asset named '/public/index.html'
|
|
497
|
-
const asset = contentAssets.getAsset('/public/index.html');
|
|
498
|
-
|
|
499
|
-
// 'wasm-inline' if object's data is 'inlined' into Wasm binary
|
|
500
|
-
// 'kv-store' if object's data exists in Fastly KV Store
|
|
501
|
-
asset.type;
|
|
502
|
-
|
|
503
|
-
// Get the "store entry"
|
|
504
|
-
const storeEntry = await asset.getStoreEntry();
|
|
505
|
-
|
|
506
|
-
storeEntry.contentEncoding; // null, 'br', 'gzip'
|
|
507
|
-
storeEntry.hash; // SHA256 of the contents of the file
|
|
508
|
-
storeEntry.size; // Size of file in bytes
|
|
509
|
-
```
|
|
510
|
-
|
|
511
|
-
Regardless of which store these objects come from, they implement the `Body` interface as defined by `@fastly/js-compute`.
|
|
512
|
-
As such, you are able to work with them in the same way to obtain their contents:
|
|
513
|
-
|
|
514
|
-
```js
|
|
515
|
-
storeEntry.body; // ReadableStream<Uint8Array>
|
|
516
|
-
storeEntry.bodyUsed; // true if consumed or distrubed
|
|
517
|
-
|
|
518
|
-
// Get the data as ArrayBuffer, parsed JSON, or string
|
|
519
|
-
// The latter two are only available if the data is a text type
|
|
520
|
-
const arrayBuffer = await storeEntry.arrayBuffer();
|
|
521
|
-
const json = await storeEntry.json();
|
|
522
|
-
const text = await storeEntry.text();
|
|
523
|
-
```
|
|
524
|
-
|
|
525
|
-
Or, if you don't care about the contents but just want to stream it to the visitor, you can pass the `.body` field directly
|
|
526
|
-
to the Response constructor:
|
|
527
|
-
|
|
528
|
-
```js
|
|
529
|
-
const response = new Response(storeEntry.body, { status: 200 });
|
|
530
|
-
```
|
|
531
|
-
|
|
532
|
-
> IMPORTANT: Once a store entry is consumed, its body cannot be read from again. If you need to access the contents of the
|
|
533
|
-
same asset more than once, you may obtain another store entry, as in the following example:
|
|
534
|
-
```js
|
|
535
|
-
import { contentAssets } from './statics';
|
|
536
|
-
const asset = contentAssets.getAsset('/public/index.html');
|
|
537
|
-
const entry1 = await asset.getStoreEntry(); // Get a new store entry
|
|
538
|
-
const json1a = await entry1.json();
|
|
539
|
-
const json1b = await entry1.json(); // Can't do this, the body has already been consumed!
|
|
540
|
-
|
|
541
|
-
const entry2 = await asset.getStoreEntry(); // Get a new store entry for same asset
|
|
542
|
-
const json2a = await entry2.json(); // This will work.
|
|
543
|
-
```
|
|
544
|
-
|
|
545
|
-
#### Module Assets
|
|
546
|
-
|
|
547
|
-
Module assets are useful when an asset includes executable JavaScript code that you may want to execute at runtime.
|
|
548
|
-
|
|
549
|
-
You can obtain the module assets included in publishing by importing the `moduleAssets` object exported from
|
|
550
|
-
`./statics.js`. Keep in mind that by default, no modules are included in `moduleAssets`. If you wish to include module
|
|
551
|
-
assets, you must configure your publishing to include them. See [`moduleAssetInclusionTest` in the `static-publish.rc.js`
|
|
552
|
-
config file](#static-publish-rc) for more details.
|
|
553
|
-
|
|
554
|
-
`/module/hello.js`
|
|
555
|
-
```js
|
|
556
|
-
export function hello() {
|
|
557
|
-
console.log('Hello, World!');
|
|
558
|
-
}
|
|
559
|
-
```
|
|
560
|
-
|
|
561
|
-
```js
|
|
562
|
-
import { moduleAssets } from './statics';
|
|
563
|
-
|
|
564
|
-
// Obtain a module asset named '/module/hello.js'
|
|
565
|
-
const asset = contentAssets.getAsset('/module/hello.js');
|
|
566
|
-
|
|
567
|
-
// Load the module
|
|
568
|
-
const helloModule = await asset.getModule();
|
|
569
|
-
|
|
570
|
-
helloModule.hello(); // Will print "Hello, World!"
|
|
571
|
-
```
|
|
572
|
-
|
|
573
|
-
#### Metadata
|
|
574
|
-
|
|
575
|
-
In some use cases, you may have a use case where you need to know about the files that were included during publishing,
|
|
576
|
-
but not in the context of Compute. (e.g., a tool that runs in Node.js that performs some maintenance task on assets).
|
|
577
|
-
|
|
578
|
-
You cannot import `./statics.js` from a Node.js application, as it holds dependencies on Compute.
|
|
579
|
-
|
|
580
|
-
Instead, you can import `./statics-metadata.js`, a companion file that is generated in the same directory. This file
|
|
581
|
-
exposes plain JavaScript objects that contain the metadata about your content assets that were included in the final
|
|
582
|
-
publishing event.
|
|
583
|
-
|
|
584
|
-
See the definition of `ContentAssetMetadataMapEntry` in the [`types/content-assets` file](./src/types/content-assets.ts) for more details.
|
|
585
|
-
|
|
586
|
-
### Using webpack
|
|
587
|
-
|
|
588
|
-
As of v4, webpack is no longer required, and is no longer part of the default scaffolded application.
|
|
589
|
-
If you wish to use some features of webpack, you may include webpack in your generated application by specifying
|
|
590
|
-
`--webpack` at the command line.
|
|
591
|
-
|
|
592
|
-
## Migrating
|
|
593
|
-
|
|
594
|
-
See [MIGRATING.md](./MIGRATING.md).
|
|
595
|
-
|
|
596
|
-
## Issues
|
|
597
|
-
|
|
598
|
-
If you encounter any non-security-related bug or unexpected behavior, please [file an issue][bug].
|
|
599
|
-
|
|
600
|
-
[bug]: https://github.com/fastly/compute-js-static-publish/issues/new?labels=bug
|
|
601
|
-
|
|
602
|
-
### Security issues
|
|
603
|
-
|
|
604
|
-
Please see our [SECURITY.md](./SECURITY.md) for guidance on reporting security-related issues.
|
|
45
|
+
- Named collections for previews, staging, production
|
|
46
|
+
- SPA + fallback handling
|
|
47
|
+
- Precompressed Brotli/gzip support
|
|
48
|
+
- CLI tools for publish, promote, and cleanup
|
|
605
49
|
|
|
606
|
-
##
|
|
50
|
+
## Documentation
|
|
607
51
|
|
|
608
|
-
|
|
52
|
+
📘 Full documentation available on GitHub:
|
|
53
|
+
[https://github.com/fastly/compute-js-static-publish](https://github.com/fastly/compute-js-static-publish)
|