@usethrottle/create-store 0.0.0-stage → 0.2.0
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/CLAUDE.md +48 -0
- package/README.md +29 -2
- package/dist/index.js +445 -0
- package/dist/index.js.map +1 -0
- package/package.json +49 -3
- package/template/.env.local.tmpl +11 -0
- package/template/.gitignore.tmpl +7 -0
- package/template/AGENTS.md +25 -0
- package/template/README.md +79 -0
- package/template/app/account/page.tsx +7 -0
- package/template/app/api/throttle/revalidate/route.ts +5 -0
- package/template/app/api/throttle/sync/route.ts +9 -0
- package/template/app/c/[handle]/page.tsx +7 -0
- package/template/app/collections/[handle]/page.tsx +7 -0
- package/template/app/layout.tsx +16 -0
- package/template/app/not-found.tsx +5 -0
- package/template/app/p/[handle]/page.tsx +11 -0
- package/template/app/page.tsx +2 -0
- package/template/app/providers.tsx +22 -0
- package/template/app/search/page.tsx +11 -0
- package/template/app/shop/page.tsx +7 -0
- package/template/app/tags/[tag]/page.tsx +7 -0
- package/template/lib/store.ts +19 -0
- package/template/next.config.ts +9 -0
- package/template/package.json.tmpl +22 -0
- package/template/proxy.ts +14 -0
- package/template/tsconfig.json +20 -0
- package/template-builder-build/.env.local.tmpl.append +11 -0
- package/template-builder-build/.gitignore.tmpl.append +3 -0
- package/template-builder-build/AGENTS.md.append +16 -0
- package/template-builder-build/README.md.append +49 -0
- package/template-builder-build/app/layout.tsx +36 -0
- package/template-builder-build/app/pages/[handle]/page.tsx +26 -0
- package/template-builder-build/app/providers.tsx +31 -0
- package/template-builder-build/app/sitemap.ts +10 -0
- package/template-builder-build/lib/builder.ts +75 -0
- package/template-builder-build/lib/packs.ts +9 -0
- package/template-builder-build/next.config.ts +61 -0
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
|
|
2
|
+
## Builder build mode
|
|
3
|
+
|
|
4
|
+
This store was created with `--builder-mode build`: its Throttle Builder pages are **generated at build time from a
|
|
5
|
+
frozen release**, and the running store never calls the Builder.
|
|
6
|
+
|
|
7
|
+
- **One full build per release, zero Builder-driven ISR writes.** In the Builder, pending changes pile up until you press
|
|
8
|
+
**Release**. That freezes everything published at that moment (site settings, every page, header, footer, slots, frozen
|
|
9
|
+
custom widgets and assets) and calls your host's deploy hook once. The hook starts a build, and `next build` reads the
|
|
10
|
+
release and statically generates every page of it (`generateStaticParams` in `app/pages/[handle]/page.tsx`,
|
|
11
|
+
`dynamicParams = false`). Nothing on a timer regenerates them and nothing in the Builder does, so a release costs the one
|
|
12
|
+
build. Catalog changes still re-render pages through the signed `/api/throttle/revalidate` webhook, exactly as in a live
|
|
13
|
+
store. This is the one place the store generates every page on purpose: one build per release, not one write per visit.
|
|
14
|
+
- **The release is a file.** `next.config.ts` reads the release from the Builder once and writes it to
|
|
15
|
+
`.builder/release.json` (git-ignored); Next copies that file into the build output, and every page, the layout (header,
|
|
16
|
+
footer, slots) and the sitemap render from it. Nothing reads the Builder at run time, not even a cold instance rendering
|
|
17
|
+
`/search` or a product page, and the running store needs no Builder key. The build workers read the file too instead of
|
|
18
|
+
downloading the bundle again. The bundle is read into memory whole (up to 48 MB), so the build machine needs that much
|
|
19
|
+
headroom, and the file ships in every server function, so mind your host's function size limit.
|
|
20
|
+
- **A store with no release file fails on purpose.** If `.builder/release.json` is missing or corrupt at run time, the
|
|
21
|
+
page that needs it throws a clear error, even when `THROTTLE_BUILDER_KEY` is set on the host (a quiet fallback would
|
|
22
|
+
download the whole release on every cold start). Run `next build` again. Only `next dev` reads from the Builder: with
|
|
23
|
+
`THROTTLE_BUILDER_KEY` set it reads a fresh release and ignores the file, without it it uses the file of the last build;
|
|
24
|
+
it logs which one. A release over about 20 MB prints a warning at build, because the file is in every page's server
|
|
25
|
+
function (the `/api` routes are left out).
|
|
26
|
+
- **Why the layout reads settings for a year.** A page lives only as long as its shortest read, and the catalog settings read
|
|
27
|
+
in `app/layout.tsx` defaults to an hour, which would have Next render the release pages again every hour. `app/layout.tsx`
|
|
28
|
+
sets `cachePolicy: { settings: '525600m' }` (a year; never under an hour, the cost rule). A catalog change still reaches
|
|
29
|
+
the pages through the webhook, and a page rendered again reads the frozen release, never a newer one.
|
|
30
|
+
- **Set up the deploy hook.** Create a deploy hook on your host (Vercel: Project Settings, Git, Deploy Hooks), paste its
|
|
31
|
+
https URL in the Builder's release settings, and switch the store to build mode there.
|
|
32
|
+
- **The build reads the newest release**, or the one named by `BUILDER_RELEASE_ID`; a whole build is one release, even if
|
|
33
|
+
someone presses Release while it runs. Rolling back in the Builder makes a new release of an older snapshot and deploys it.
|
|
34
|
+
Set `BUILDER_RELEASE_ID` to rebuild a specific release by hand.
|
|
35
|
+
- **Keys.** `THROTTLE_BUILDER_KEY` is a build key (the publishable key of a server channel, or a secret key with
|
|
36
|
+
`releases:read`). It is for `next build` only; never put it in a `NEXT_PUBLIC_` variable. `THROTTLE_MEDIA_ORIGIN` is the
|
|
37
|
+
https origin your Builder images come from (and the only origin an og:image may be on).
|
|
38
|
+
- **Search and sharing.** Each Builder page's search title, description, share image and "hide from search engines" reach the
|
|
39
|
+
page's `<head>` (`generateMetadata` in `app/pages/[handle]/page.tsx`), and `app/sitemap.ts` leaves hidden pages out. The
|
|
40
|
+
canonical link and the sitemap are built from `NEXT_PUBLIC_SITE_URL` (your store's own origin); without it they point at
|
|
41
|
+
`http://localhost:3000`. The Builder emits no structured data (JSON-LD): add it in your own metadata if you want it.
|
|
42
|
+
- **The announcement bar** is switched on and off by its start and end time when a page is rendered. The Builder pages are
|
|
43
|
+
generated once, at build, so on them a start or end time that passes after the build only takes effect with the next
|
|
44
|
+
release. Catalog pages (shop, search, products) are rendered per request, and the bar changes on them at its times.
|
|
45
|
+
- **Pages** are at `/pages/<slug>`. The release's home page is at `/pages/home` (`/` stays the shop; point `app/page.tsx` at
|
|
46
|
+
the home page if you want it at `/`), and `app/sitemap.ts` lists it there. A renamed page answers its old
|
|
47
|
+
`/pages/<old-slug>` with a permanent redirect (`redirects()` in `next.config.ts`).
|
|
48
|
+
- Keep the cost rules above: no `revalidate` under 3600, no `revalidatePath`, and do not call the Builder from the
|
|
49
|
+
browser (`BuilderProvider` gets no key on purpose).
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import '@usethrottle/storefront-react/styles.css';
|
|
2
|
+
import { CATALOG_SLOT_POINTS } from '@usethrottle/storefront-react/builder';
|
|
3
|
+
import { createStorefrontServerClient, loadSettings } from '@usethrottle/storefront-react/server';
|
|
4
|
+
import { loadSite } from '@usethrottle/builder-react/server';
|
|
5
|
+
import type { ReactNode } from 'react';
|
|
6
|
+
import { builder, defaultLocale } from '@/lib/builder';
|
|
7
|
+
import { createPacks } from '@/lib/packs';
|
|
8
|
+
import { store } from '@/lib/store';
|
|
9
|
+
import { Providers } from './providers';
|
|
10
|
+
|
|
11
|
+
export default async function RootLayout({ children }: { children: ReactNode }) {
|
|
12
|
+
const locale = await defaultLocale();
|
|
13
|
+
const [settings, site] = await Promise.all([
|
|
14
|
+
// The settings read sits in every page's layout, and a page lives only as long as its shortest read. The default
|
|
15
|
+
// is an hour, which would make Next render the release pages again every hour. A year (525600 minutes) keeps them
|
|
16
|
+
// as built; a catalog change still reaches them through the signed revalidate webhook, as in a live store.
|
|
17
|
+
loadSettings({ client: createStorefrontServerClient({ catalogKey: store.catalogKey, cachePolicy: { settings: '525600m' } }) }),
|
|
18
|
+
// Read from the frozen release file: no request to the Builder, at build or at run time. mediaOrigin is the one origin Builder images may be on.
|
|
19
|
+
loadSite({
|
|
20
|
+
client: builder,
|
|
21
|
+
locale,
|
|
22
|
+
slots: CATALOG_SLOT_POINTS.map((slot) => slot.name),
|
|
23
|
+
packs: createPacks(),
|
|
24
|
+
mediaOrigin: process.env.THROTTLE_MEDIA_ORIGIN ?? '',
|
|
25
|
+
}),
|
|
26
|
+
]);
|
|
27
|
+
return (
|
|
28
|
+
<html lang={locale}>
|
|
29
|
+
<body style={{ margin: 0 }}>
|
|
30
|
+
<Providers settings={settings} site={site}>
|
|
31
|
+
{children}
|
|
32
|
+
</Providers>
|
|
33
|
+
</body>
|
|
34
|
+
</html>
|
|
35
|
+
);
|
|
36
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { CATALOG_SLOT_POINTS } from '@usethrottle/storefront-react/builder';
|
|
2
|
+
import { createBuilderPage } from '@usethrottle/builder-react/next';
|
|
3
|
+
import { builderStaticParams } from '@usethrottle/builder-react/server';
|
|
4
|
+
import { builder, defaultLocale } from '@/lib/builder';
|
|
5
|
+
import { createPacks } from '@/lib/packs';
|
|
6
|
+
import { store } from '@/lib/store';
|
|
7
|
+
|
|
8
|
+
// Build mode: every page of the release is generated when you build and none is rendered again by time, so these pages
|
|
9
|
+
// cost no ISR write after the deploy (see the settings read in app/layout.tsx). A slug that is not in the release is a 404.
|
|
10
|
+
export const generateStaticParams = () => builderStaticParams({ client: builder });
|
|
11
|
+
export const dynamicParams = false;
|
|
12
|
+
|
|
13
|
+
const route = createBuilderPage({
|
|
14
|
+
fromParams: 'handle',
|
|
15
|
+
client: builder,
|
|
16
|
+
packs: createPacks(),
|
|
17
|
+
locale: defaultLocale,
|
|
18
|
+
slots: CATALOG_SLOT_POINTS.map((slot) => slot.name),
|
|
19
|
+
// The canonical link and the share image (og:image): your store's own origin (NEXT_PUBLIC_SITE_URL) and the origin
|
|
20
|
+
// your Builder images are served from (THROTTLE_MEDIA_ORIGIN). Without the media origin a page has no og:image.
|
|
21
|
+
storeUrl: store.baseUrl,
|
|
22
|
+
mediaOrigin: process.env.THROTTLE_MEDIA_ORIGIN ?? '',
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
export default route.Page;
|
|
26
|
+
export const generateMetadata = route.generateMetadata;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use client';
|
|
2
|
+
import { BuilderProvider } from '@usethrottle/builder-react';
|
|
3
|
+
import type { LoadedSite } from '@usethrottle/builder-react';
|
|
4
|
+
import { StoreFrame } from '@usethrottle/storefront-react';
|
|
5
|
+
import { StorefrontNextProvider, type StorefrontNextProviderProps } from '@usethrottle/storefront-react/next/client';
|
|
6
|
+
import type { ReactNode } from 'react';
|
|
7
|
+
import { createPacks } from '@/lib/packs';
|
|
8
|
+
|
|
9
|
+
const packs = createPacks();
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The storefront provider, the Builder's site design (menus, brand, announcement, theme, slots) and the page around
|
|
13
|
+
* every route. `BuilderProvider` gets no key and no client: the site was read from the release at build time, so the
|
|
14
|
+
* browser never calls the Builder.
|
|
15
|
+
* `generationWatch` is the open tab's check for a catalog change: one cheap conditional read at this interval.
|
|
16
|
+
* 60 s is the least the package allows. Raise it if you like; do not lower it.
|
|
17
|
+
*/
|
|
18
|
+
export function Providers({ settings, site, children }: { settings: StorefrontNextProviderProps['settings']; site: LoadedSite; children: ReactNode }) {
|
|
19
|
+
return (
|
|
20
|
+
<StorefrontNextProvider
|
|
21
|
+
catalogKey={process.env.NEXT_PUBLIC_THROTTLE_CATALOG_KEY}
|
|
22
|
+
settings={settings}
|
|
23
|
+
generationWatch={{ intervalMs: 60_000 }}
|
|
24
|
+
injectStyles={false}
|
|
25
|
+
>
|
|
26
|
+
<BuilderProvider packs={packs} site={site}>
|
|
27
|
+
<StoreFrame>{children}</StoreFrame>
|
|
28
|
+
</BuilderProvider>
|
|
29
|
+
</StorefrontNextProvider>
|
|
30
|
+
);
|
|
31
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { MetadataRoute } from 'next';
|
|
2
|
+
import { builderSitemap } from '@usethrottle/builder-react/server';
|
|
3
|
+
import { builder, defaultLocale } from '@/lib/builder';
|
|
4
|
+
import { store } from '@/lib/store';
|
|
5
|
+
|
|
6
|
+
// The release's pages, read from the frozen release at build, without the pages set to noindex. The Builder's home page is
|
|
7
|
+
// served at /pages/home (`/` is the shop), and the sitemap lists it there, as its canonical link says.
|
|
8
|
+
export default async function sitemap(): Promise<MetadataRoute.Sitemap> {
|
|
9
|
+
return builderSitemap({ client: builder, storeUrl: store.baseUrl, locale: await defaultLocale() });
|
|
10
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { readFileSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import { createBuilderClient, type ReleaseBundle } from '@usethrottle/builder';
|
|
4
|
+
|
|
5
|
+
// Build mode: the Builder is read ONCE, by `next build` (next.config.ts), and written to `.builder/release.json`. This
|
|
6
|
+
// client renders from that file, so pages, layouts and the sitemap make no Builder request at build or at run time, and
|
|
7
|
+
// a running store needs no Builder key.
|
|
8
|
+
//
|
|
9
|
+
// A built or running store with no readable release file FAILS, even when THROTTLE_BUILDER_KEY is set on the host: a
|
|
10
|
+
// quiet fallback would download the whole release (up to 48 MB) on every cold start. Only `next dev` reads from the
|
|
11
|
+
// Builder, and only when the key is set: the publishable key of a SERVER channel (tb_pk_...), or a secret key holding
|
|
12
|
+
// releases:read. Never a NEXT_PUBLIC_ variable.
|
|
13
|
+
const FROZEN = join(process.cwd(), '.builder', 'release.json');
|
|
14
|
+
const HELD = Symbol.for('throttle.builder.frozen-release');
|
|
15
|
+
const SAID = Symbol.for('throttle.builder.source-said');
|
|
16
|
+
const development = process.env.NODE_ENV === 'development';
|
|
17
|
+
const builderKey = process.env.THROTTLE_BUILDER_KEY;
|
|
18
|
+
|
|
19
|
+
/** The frozen release, or undefined when there is no file. A file that cannot be read or parsed throws. Parsed once per process (it can be tens of MB). */
|
|
20
|
+
function frozen(): ReleaseBundle | undefined {
|
|
21
|
+
const holder = globalThis as unknown as Record<symbol, ReleaseBundle | undefined>;
|
|
22
|
+
if (holder[HELD] !== undefined) return holder[HELD];
|
|
23
|
+
let text: string;
|
|
24
|
+
try {
|
|
25
|
+
text = readFileSync(FROZEN, 'utf8');
|
|
26
|
+
} catch (error) {
|
|
27
|
+
if ((error as { code?: string }).code === 'ENOENT') return undefined;
|
|
28
|
+
throw new Error(`Cannot read the frozen Builder release ${FROZEN}: ${(error as Error).message}. Run \`next build\` again.`, { cause: error });
|
|
29
|
+
}
|
|
30
|
+
let parsed: unknown;
|
|
31
|
+
try {
|
|
32
|
+
parsed = JSON.parse(text);
|
|
33
|
+
} catch (error) {
|
|
34
|
+
throw new Error(`The frozen Builder release ${FROZEN} is corrupt (it is not valid JSON). Delete the .builder folder and run \`next build\` again.`, { cause: error });
|
|
35
|
+
}
|
|
36
|
+
const bundle = parsed as Partial<ReleaseBundle> | null;
|
|
37
|
+
if (typeof bundle?.releaseId !== 'string' || !Array.isArray(bundle.pages))
|
|
38
|
+
throw new Error(`The frozen Builder release ${FROZEN} is not a release bundle. Delete the .builder folder and run \`next build\` again.`);
|
|
39
|
+
holder[HELD] = bundle as ReleaseBundle;
|
|
40
|
+
return holder[HELD];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** `next dev` with a key reads a fresh release, never a stale file. Say so, once, so nobody wonders which release they are looking at. */
|
|
44
|
+
function say(message: string): void {
|
|
45
|
+
const holder = globalThis as unknown as Record<symbol, boolean | undefined>;
|
|
46
|
+
if (holder[SAID]) return;
|
|
47
|
+
holder[SAID] = true;
|
|
48
|
+
console.warn(`[throttle builder] ${message}`);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const readsTheBuilder = development && builderKey !== undefined && builderKey !== '';
|
|
52
|
+
const releaseBundle = readsTheBuilder ? undefined : frozen();
|
|
53
|
+
|
|
54
|
+
if (releaseBundle === undefined && !readsTheBuilder)
|
|
55
|
+
throw new Error(
|
|
56
|
+
development
|
|
57
|
+
? 'No frozen Builder release (.builder/release.json) and no THROTTLE_BUILDER_KEY: set the key in .env.local for `next dev`, or run `next build` once.'
|
|
58
|
+
: 'No frozen Builder release (.builder/release.json). `next build` writes it from the Builder (it needs THROTTLE_BUILDER_KEY), and a running store never reads the Builder itself. Run `next build` again.',
|
|
59
|
+
);
|
|
60
|
+
|
|
61
|
+
if (development) {
|
|
62
|
+
say(
|
|
63
|
+
releaseBundle === undefined
|
|
64
|
+
? 'next dev: reading the release from the Builder (THROTTLE_BUILDER_KEY is set; the .builder/release.json of the last build is ignored).'
|
|
65
|
+
: `next dev: no THROTTLE_BUILDER_KEY, so rendering the release ${releaseBundle.releaseId} frozen by the last build (.builder/release.json), which may be stale.`,
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export const builder =
|
|
70
|
+
releaseBundle !== undefined
|
|
71
|
+
? createBuilderClient({ mode: 'build', releaseBundle })
|
|
72
|
+
: createBuilderClient({ mode: 'build', builderKey: builderKey!, release: process.env.BUILDER_RELEASE_ID });
|
|
73
|
+
|
|
74
|
+
/** The release's default locale: the store is generated in it. */
|
|
75
|
+
export const defaultLocale = async (): Promise<string> => (await builder.getReleaseBundle()).defaultLocale;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { catalogPack, commercePack } from '@usethrottle/storefront-react/builder';
|
|
2
|
+
|
|
3
|
+
// The widget packs the Builder's catalog and commerce widgets are drawn with. A plain module (no "use client"):
|
|
4
|
+
// the server passes the packs to loadSite() and the browser module of app/providers.tsx passes them to the provider.
|
|
5
|
+
// The server reads with the server key; in the browser that variable is empty and the browser key is used.
|
|
6
|
+
export const createPacks = () => [
|
|
7
|
+
catalogPack({ catalogKey: process.env.THROTTLE_CATALOG_SERVER_KEY || process.env.NEXT_PUBLIC_THROTTLE_CATALOG_KEY || '' }),
|
|
8
|
+
commercePack(),
|
|
9
|
+
];
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { join } from 'node:path';
|
|
3
|
+
import type { NextConfig } from 'next';
|
|
4
|
+
import { PHASE_PRODUCTION_BUILD } from 'next/constants';
|
|
5
|
+
import { createBuilderClient } from '@usethrottle/builder';
|
|
6
|
+
|
|
7
|
+
// Nothing here sets a cache time. Build mode generates every page of one Builder release at build time. See README.md.
|
|
8
|
+
//
|
|
9
|
+
// The release is read ONCE, here, and written to `.builder/release.json`. Next copies that file into the build output
|
|
10
|
+
// (`outputFileTracingIncludes`), `lib/builder.ts` renders every page, layout and sitemap from it, and the running store
|
|
11
|
+
// never calls the Builder (and needs no Builder key). The build workers read the same file instead of downloading the
|
|
12
|
+
// bundle again.
|
|
13
|
+
const FROZEN = join(process.cwd(), '.builder', 'release.json');
|
|
14
|
+
const FROZEN_ID = join(process.cwd(), '.builder', 'release.id');
|
|
15
|
+
/** The file ships in every server function that renders a page, so a big release can outgrow a host's function size limit. */
|
|
16
|
+
const WARN_BYTES = 20 * 1024 * 1024;
|
|
17
|
+
|
|
18
|
+
/** Pin the release of this build: BUILDER_RELEASE_ID if set, else the newest. Runs in the build's main process; its workers find the file and skip. */
|
|
19
|
+
async function freeze(): Promise<void> {
|
|
20
|
+
const wanted = process.env.BUILDER_RELEASE_ID;
|
|
21
|
+
if (wanted && existsSync(FROZEN) && existsSync(FROZEN_ID) && readFileSync(FROZEN_ID, 'utf8') === wanted) return;
|
|
22
|
+
const builderKey = process.env.THROTTLE_BUILDER_KEY;
|
|
23
|
+
if (!builderKey) throw new Error('Set THROTTLE_BUILDER_KEY (your Throttle Builder build key) for `next build`: the build reads the release from the Builder once.');
|
|
24
|
+
const bundle = await createBuilderClient({ mode: 'build', builderKey }).getReleaseBundle(wanted ? { release: wanted } : undefined);
|
|
25
|
+
mkdirSync(join(process.cwd(), '.builder'), { recursive: true });
|
|
26
|
+
writeFileSync(`${FROZEN}.tmp`, JSON.stringify(bundle));
|
|
27
|
+
renameSync(`${FROZEN}.tmp`, FROZEN);
|
|
28
|
+
writeFileSync(FROZEN_ID, bundle.releaseId);
|
|
29
|
+
const bytes = statSync(FROZEN).size;
|
|
30
|
+
if (bytes > WARN_BYTES)
|
|
31
|
+
console.warn(
|
|
32
|
+
`[throttle builder] The frozen release is ${(bytes / 1024 / 1024).toFixed(1)} MB. It is copied into every server function that renders a page, so check your host's function size limit (and the build machine's memory). Fewer or lighter pages in the release keep it small.`,
|
|
33
|
+
);
|
|
34
|
+
// Before Next starts its build workers, so every page of this build comes from the same release even if someone
|
|
35
|
+
// presses Release while it runs.
|
|
36
|
+
process.env.BUILDER_RELEASE_ID = bundle.releaseId;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** A renamed page answers its old address with a permanent redirect. `dynamicParams = false` would 404 it first. */
|
|
40
|
+
function renamedPages(): { source: string; destination: string; permanent: true }[] {
|
|
41
|
+
if (!existsSync(FROZEN)) return [];
|
|
42
|
+
const { redirects } = JSON.parse(readFileSync(FROZEN, 'utf8')) as { redirects?: Record<string, string> };
|
|
43
|
+
return Object.entries(redirects ?? {})
|
|
44
|
+
// A path on this site only, as in createBuilderPage.
|
|
45
|
+
.filter(([, to]) => to.startsWith('/') && !to.startsWith('//'))
|
|
46
|
+
.map(([from, to]) => ({ source: `/pages/${from.replace(/[\\:()*+?{}[\]]/gu, '\\$&')}`, destination: to, permanent: true as const }));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export default async function config(phase: string): Promise<NextConfig> {
|
|
50
|
+
if (phase === PHASE_PRODUCTION_BUILD) await freeze();
|
|
51
|
+
return {
|
|
52
|
+
poweredByHeader: false,
|
|
53
|
+
// Next inlines this into the build, so a page it renders again later (see README.md) reads the release this build
|
|
54
|
+
// was made from, never a newer one.
|
|
55
|
+
env: { BUILDER_RELEASE_ID: process.env.BUILDER_RELEASE_ID ?? '' },
|
|
56
|
+
// Every route that renders a page needs the release (the root layout reads it); the signed webhook routes under
|
|
57
|
+
// /api do not, so they stay small. A new route outside /api that renders the layout is covered.
|
|
58
|
+
outputFileTracingIncludes: { '/!(api)/**': ['./.builder/release.json'] },
|
|
59
|
+
redirects: () => Promise.resolve(renamedPages()),
|
|
60
|
+
};
|
|
61
|
+
}
|