Skip to content

Commit 077af28

Browse files
committed
More v7 readme updates
1 parent dae3360 commit 077af28

File tree

2 files changed

+163
-27
lines changed

2 files changed

+163
-27
lines changed

README.md

Lines changed: 152 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
- [✨ Key Features](#-key-features)
66
- [🏁 Quick Start](#-quick-start)
77
- [⚙️ Configuring `static-publish.rc.js`](#️-configuring-static-publishrcjs)
8-
- [🧾 Default Server Config: `publish-content.config.js`](#-default-server-config-publish-contentconfigjs)
8+
- [🧾 Default Server Config: `publish-content.config.js`](#-publish-and-server-config-publish-contentconfigjs)
99
- [📦 Collections (Publish, Preview, Promote)](#-collections-publish-preview-promote)
1010
- [🧹 Cleaning Up](#-cleaning-up)
1111
- [🔄 Content Compression](#-content-compression)
@@ -116,31 +116,32 @@ const rc = {
116116
kvStoreName: 'site-content',
117117
defaultCollectionName: 'live',
118118
publishId: 'default',
119+
staticPublisherWorkingDir: './static-publisher',
119120
};
120121

121122
export default rc;
122123
```
123124

124125
### Fields:
125126

126-
- `kvStoreName` - The name of the KV Store used for publishing (required).
127-
- `defaultCollectionName` - Collection to serve when none is specified (required).
128-
- `publishId` - Unique prefix for all keys in the KV Store (required). Override only for advanced setups (e.g., multiple apps sharing the same KV Store).
127+
All fields are required.
128+
129+
- `kvStoreName` - The name of the KV Store used for publishing.
130+
- `defaultCollectionName` - Collection to serve when none is specified.
131+
- `publishId` - Unique prefix for all keys in the KV Store. Override only for advanced setups (e.g., multiple apps sharing the same KV Store).
132+
- `staticPublisherWorkingDir` - Directory to hold working files during publish.
129133

130134
> [!NOTE]
131135
> Changes to this file require rebuilding the Compute app, since a copy of it is baked into the Wasm binary.
132136
133-
## 🧾 Default Server Config: `publish-content.config.js`
137+
## 🧾 Publish and Server Config: `publish-content.config.js`
134138

135139
This file is included as part of the scaffolding. Every time you publish content, the publish settings in this file are used for publishing the content, and the server settings are taken from this file and saved as the settings used by the server for that collection.
136140

137-
You can override this file for a single `publish-content` command by specifying an alternative using `--config` on the command line.
138-
139141
```js
140142
const config = {
141143
// these paths are relative to compute-js dir
142144
rootDir: '../public',
143-
staticPublisherWorkingDir: './static-publisher',
144145

145146
// Include/exclude filters (optional):
146147
excludeDirs: ['node_modules'],
@@ -175,13 +176,11 @@ const config = {
175176
export default config;
176177
```
177178

178-
> [!NOTE]
179-
> Changes to this file apply when content is published.
179+
You can override this file for a single `publish-content` command by specifying an alternative using `--config` on the command line.
180180

181181
### Fields:
182182

183183
- `rootDir` - Directory to scan for content, relative to this file (required).
184-
- `staticPublisherWorkingDir` - Directory to hold working files during publish (default: `'./static-publisher'`).
185184
- `excludeDirs` - Array of directory names or regex patterns to exclude (default: `['./node_modules']`).
186185
- `excludeDotFiles` - Exclude dotfiles and dot-named directories (default: true).
187186
- `includeWellKnown` - Always include `.well-known` even if dotfiles are excluded (default: true).
@@ -225,18 +224,19 @@ You can overwrite or republish any collection at any time. Old file hashes will
225224

226225
Collections can expire automatically:
227226

228-
- Expired collections return 404s
229-
- They’re ignored by the server
230-
- Their files are cleaned up by `clean`
227+
- Expired collections are ignored by the server and return 404s
228+
- Expiration limits can be modified (shortened, extended, reenstated) using `collections update-expiration`
229+
- They are cleaned up by `clean --delete-expired-collections`
231230

232231
```sh
233232
--expires-in=3d # relative (e.g. 1h, 2d, 1w)
234233
--expires-at=2025-05-01T12:00Z # absolute (ISO 8601)
234+
--expires-never
235235
```
236236

237237
### Switching the active collection
238238

239-
By default, the app serves from the collection named in `static-publish
239+
By default, the server app serves assets from the "default collection", named in `static-publish
240240
.rc.js` under `defaultCollectionName`. To switch the active collection, you add custom code to your Compute app that calls `publisherServer.setActiveCollectionName(name)`:
241241

242242
```js
@@ -286,6 +286,8 @@ collectionSelector.fromRequest(request, req => req.headers.get('x-collection') ?
286286
287287
#### From a Cookie
288288
289+
See [fromCookie](#from-cookie) for details on this feature.
290+
289291
```js
290292
const { collectionName, redirectResponse } = collectionSelector.fromCookie(request);
291293
```
@@ -303,9 +305,10 @@ If you're happy with a preview or staging collection and want to make it live, u
303305
```sh
304306
npx @fastly/compute-js-static-publish collections promote \
305307
--collection-name=staging
308+
--to=live
306309
```
307310
308-
This copies all content and server settings from the `staging` collection to the collection named in your `defaultCollectionName`. To copy to a different name, add `--to=some-other-name`.
311+
This copies all content and server settings from the `staging` collection to `live`.
309312
310313
You can also specify a new expiration:
311314
@@ -321,12 +324,33 @@ npx @fastly/compute-js-static-publish collections promote \
321324
322325
## 🛠 Development → Production Workflow
323326
324-
During development, starting the local preview server (`npm run start`) will run `publish-content --local-only` automatically via a `prestart hook`. This simulates publishing by writing to `kvstore.json` instead of uploading to the actual KV Store. You can preview your site at `http://127.0.0.1:7676` - no Fastly account or service required.
327+
### Local development
328+
329+
During development, the local preview server (`npm run dev:start`) will run against assets loaded into the simulated KV Store provided by the local development environment.
330+
331+
Prior to starting the server, publish the content to the simulated KV Store:
332+
333+
```sh
334+
npm run dev:publish # 'publish' your files to the simulated local KV Store
335+
npm run dev:start # preview locally
336+
```
337+
338+
This simulates publishing by writing to `kvstore.json` instead of uploading to the actual KV Store. You can preview your site at `http://127.0.0.1:7676` - no Fastly account or service required.
339+
340+
Note that for local development, you will have to stop and restart the local development server each time you publish updates to your content.
341+
342+
To publish to an alternative collection name, use:
343+
344+
```sh
345+
npm run dev:publish -- --collection-name=preview-123
346+
```
347+
348+
### Production
325349

326350
When you're ready for production:
327351
328352
1. [Create a free Fastly account](https://www.fastly.com/signup/?tier=free) if you haven't already.
329-
2. Run `npm run publish-service`
353+
2. Run `npm run fastly:deploy`
330354
- This builds your Compute app into a Wasm binary
331355
- Deploys it to a new or existing Fastly Compute service
332356
- If creating a new service:
@@ -336,7 +360,7 @@ When you're ready for production:
336360
Once deployed, publish content like so:
337361
338362
```sh
339-
npm run publish-content
363+
npm run fastly:publish
340364
```
341365
342366
This:
@@ -348,7 +372,7 @@ This:
348372
> [!TIP]
349373
> Upload to a specific collection by specifying the collection name when publishing content:
350374
> ```sh
351-
> npm run publish-content -- --collection-name=preview-42
375+
> npm run fastly:publish -- --collection-name=preview-42
352376
> ```
353377
354378
**No Wasm redeploy needed** unless you:
@@ -359,14 +383,25 @@ This:
359383
If you do need to rebuild and redeploy the Compute app, simply run:
360384
361385
```sh
362-
npm run publish-service
386+
npm run dev:deploy
363387
```
364388
365389
## 🧹 Cleaning Up
366390
367-
Every time you publish, old files are left behind for safety. **However, files with the same content will be re-used across collections and publishing events** - they are only stored once in the KV Store using their content hash as a key. This ensures that unchanged files aren't duplicated, keeping storage efficient and deduplicated. To avoid bloat, use:
391+
Every time you publish, old files are left behind for safety. **However, files with the same content will be re-used across collections and publishing events.** They are only stored once in the KV Store using their content hash as a key. This ensures that unchanged files aren't duplicated, keeping storage efficient and deduplicated.
392+
393+
Over time, however, collections may expire, old versions of files will be left behind, and some assets in the KV Store will no longer be referenced by any live collection. To avoid bloat, use:
368394

369395
```sh
396+
npm run dev:clean
397+
```
398+
and
399+
```sh
400+
npm run fastly:clean
401+
```
402+
403+
These scripts run against the local and Fastly KV Stores respectively, and run the following command:
404+
```
370405
npx @fastly/compute-js-static-publish clean --delete-expired-collections
371406
```
372407

@@ -490,10 +525,10 @@ npx @fastly/compute-js-static-publish \
490525
--root-dir=./public \
491526
--kv-store-name=site-content \
492527
[--output=./compute-js] \
528+
[--static-publisher-working-dir=<output>/static-publisher] \
493529
[--publish-id=<prefix>] \
494530
[--public-dir=./public] \
495531
[--static-dir=./public/static] \
496-
[--static-publisher-working-dir=./compute-js/static-publisher] \
497532
[--spa=./public/spa.html] \
498533
[--not-found-page=./public/404.html] \
499534
[--auto-index=index.html,index.htm] \
@@ -509,6 +544,7 @@ npx @fastly/compute-js-static-publish \
509544
**Used to generate the Compute app:**
510545
- `--kv-store-name`: Required. Name of KV Store to use.
511546
- `--output`: Compute app destination (default: `./compute-js`)
547+
- `--static-publisher-working-dir`: Directory to hold working files (default: `<output>/static-publisher`).
512548
- `--name`: Application name to insert into `fastly.toml`
513549
- `--description`: Application description to insert into `fastly.toml`
514550
- `--author`: Author to insert into `fastly.toml`
@@ -517,7 +553,6 @@ npx @fastly/compute-js-static-publish \
517553
**Used in building config files:**
518554
- `--root-dir`: Required. Directory of static site content.
519555
- `--publish-id`: Optional key prefix for KV entries (default: `'default'`).
520-
- `--static-publisher-working-dir`: Directory to hold working files (default: `<output>/static-publisher`).
521556
- `--public-dir`: Public files base directory (default: same as `--root-dir`).
522557
- `--static-dir`: One or more directories to serve with long cache TTLs.
523558
- `--spa`: SPA fallback file path (e.g., `./public/spa.html`).
@@ -637,6 +672,99 @@ Deletes a specific collection’s index and associated settings.
637672
638673
---
639674
675+
## Appendix
676+
677+
### fromCookie
678+
679+
`collectionSelector.fromCookie` is a utility function that enables the use of a browser
680+
cookie to select the active collection.
681+
682+
```js
683+
import { collectionSelector } from '@fastly/compute-js-static-publish';
684+
685+
const { collectionName, redirectResponse } =
686+
collectionSelector.fromCookie(request, {
687+
// Everything here is optional – these are just examples
688+
cookieName: 'publisher-collection', // default
689+
activatePath: '/activate', // default
690+
resetPath: '/reset', // default
691+
cookieHttpOnly: true, // default
692+
cookieMaxAge: 60 * 60 * 24 * 7, // 7-day preview
693+
cookiePath: '/', // default
694+
});
695+
696+
if (redirectResponse) {
697+
// honor the redirect
698+
return redirectResponse;
699+
}
700+
701+
// `collectionName` now holds the active collection (or `null` if none)
702+
```
703+
704+
#### What it does, in plain English
705+
706+
1. Reads a cookie
707+
It looks for a cookie named cookieName (default `publisher-collection`) and hands you the value as `collectionName`.
708+
709+
2. Handles two helper endpoints for you
710+
711+
| Endpoint | Purpose | Query params | Result |
712+
|------------------------------------|------------------|------------------------------------------------------------------------------------------------|-------------------------------------------|
713+
| activatePath (default `/activate`) | Set the cookie | `collection` (required) - name of the collection<br /> `redirectTo` (optional, `/` by default) | `302` redirect with a `Set-Cookie` header |
714+
| resetPath (default `/reset`) | Clear the cookie | `redirectTo` (optional, `/` by default) | `302` redirect that expires the cookie |
715+
716+
If a visitor accesses `/activate?collection=blue&redirectTo=/preview`, the helper will issue a redirect and drop `publisher-collection=blue` into their cookie jar.
717+
- If someone forgets `?collection=?`? Then `/activate` replies with HTTP `400`.
718+
719+
When the visitor hits `/reset`, the cookie is deleted.
720+
721+
3. Safety flags are baked in
722+
723+
- `HttpOnly` is on by default (configurable), to help avoid XSS issues.
724+
- `Secure` is automatically added on HTTPS requests.
725+
- `SameSite=Lax` is always set – reasonable default for previews.
726+
727+
#### Option reference
728+
729+
| Option | Type | Default | Notes |
730+
|------------------|-------------------|--------------------------|------------------------------------------------|
731+
| `cookieName` | string | `'publisher-collection'` | Name of the cookie to read/write |
732+
| `activatePath` | string | `'/activate'` | Path that sets the cookie |
733+
| `resetPath` | string | `'/reset'` | Path that clears the cookie |
734+
| `cookieHttpOnly` | boolean | `true` | Turn off if the client-side needs to read it |
735+
| `cookieMaxAge` | number\|undefined | `undefined` | Seconds until expiry; omit for session cookies |
736+
| `cookiePath` | string | `'/'` | Path attribute in the cookie |
737+
738+
#### Example
739+
740+
`fromCookie` can be dropped into a Fastly Compute app:
741+
742+
```js
743+
async function handleRequest(event) {
744+
const request = event.request;
745+
746+
// --- Cookie handling starts here
747+
const { collectionName, redirectResponse } = collectionSelector.fromCookie(request);
748+
if (redirectResponse) {
749+
// obey redirect first
750+
return redirectResponse;
751+
}
752+
if (collectionName != null) {
753+
publisherServer.setActiveCollectionName(collectionName);
754+
}
755+
// --- Cookie handling ends here
756+
757+
// Regular routing follows...
758+
const response = await publisherServer.serveRequest(request);
759+
if (response != null) {
760+
return response;
761+
}
762+
return new Response('Not found', { status: 404 });
763+
}
764+
```
765+
766+
---
767+
640768
## 📚 Next Steps
641769
642770
- View CLI command help: `npx @fastly/compute-js-static-publish --help`

README.short.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ npx @fastly/compute-js-static-publish --root-dir=./public --kv-store-name=site-c
2525
```sh
2626
cd compute-js
2727
npm install
28-
npm run start # preview locally
28+
npm run dev:publish # 'publish' your files to the simulated local KV Store
29+
npm run dev:start # preview locally
2930
```
3031

3132
Serves your app at `http://127.0.0.1:7676`, powered by a simulated KV Store.
@@ -36,8 +37,15 @@ When you're ready to go live, [create a free Fastly account](https://www.fastly.
3637

3738
```sh
3839
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")
40+
npm run fastly:deploy # deploy the app
41+
npm run fastly:publish # upload your static files
42+
```
43+
44+
In the future, unless you have further changes to make to your app itself, you can
45+
upload further updates to your static files:
46+
```sh
47+
cd compute-js
48+
npm run fastly:publish # upload your static files
4149
```
4250

4351
## Features

0 commit comments

Comments
 (0)