@eldrajs/nuxt 0.0.0-stage → 0.1.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/CHANGELOG.md +13 -0
- package/LICENSE +21 -0
- package/README.md +70 -2
- package/dist/module.cjs +5 -0
- package/dist/module.d.mts +15 -0
- package/dist/module.d.ts +15 -0
- package/dist/module.json +12 -0
- package/dist/module.mjs +89 -0
- package/dist/runtime/middleware.d.ts +2 -0
- package/dist/runtime/middleware.js +29 -0
- package/dist/runtime/paths.d.ts +4 -0
- package/dist/runtime/paths.js +15 -0
- package/dist/runtime/server/built.d.ts +29 -0
- package/dist/runtime/server/built.js +18 -0
- package/dist/runtime/server/middleware.d.ts +2 -0
- package/dist/runtime/server/middleware.js +45 -0
- package/dist/runtime/server/utils.d.ts +10 -0
- package/dist/runtime/server/utils.js +22 -0
- package/dist/runtime/unbuilt.d.ts +5 -0
- package/dist/runtime/unbuilt.js +7 -0
- package/dist/runtime/virtual.d.ts +4 -0
- package/dist/types.d.mts +7 -0
- package/dist/types.d.ts +7 -0
- package/package.json +53 -3
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# @eldrajs/nuxt changelog
|
|
2
|
+
|
|
3
|
+
Hand-maintained: every change a consumer can see gets a line under Unreleased in the same change.
|
|
4
|
+
Release-please writes the generated notes from commit messages and does not replace this.
|
|
5
|
+
|
|
6
|
+
## Unreleased
|
|
7
|
+
|
|
8
|
+
- **First release (WS-1236).** A Nuxt module for prerendered storefronts on Cloudflare Workers: a
|
|
9
|
+
request is answered from the files the build wrote; a page path with no built file is a 404 with
|
|
10
|
+
the site's error page and no API reads, any other unbuilt path a bare 404, a write to it a 405.
|
|
11
|
+
Server routes (catch-all ones included) and `prerender: false` pages are still rendered on
|
|
12
|
+
request; a visitor's request to `/__nuxt_error` is a bare 404. Only under a Cloudflare preset. `isUnbuiltPage()` lets
|
|
13
|
+
a plugin skip its reads on that 404; `fetchBuiltFile()` serves a built file from site middleware.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Eldra Tech
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,71 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @eldrajs/nuxt
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Nuxt module for Eldra storefronts that prerender their pages and run on Cloudflare Workers
|
|
4
|
+
(`nitro.preset: 'cloudflare_module'` with the `ASSETS` binding).
|
|
5
|
+
|
|
6
|
+
The site is what the build wrote. A request for a file the build wrote is answered from the asset
|
|
7
|
+
store. Anything else is answered before Nuxt renders a page or reads the Eldra API:
|
|
8
|
+
|
|
9
|
+
| Request | Answer |
|
|
10
|
+
| --------------------------------------------------------- | --------------------------------------------------- |
|
|
11
|
+
| A page or file the build wrote | The built file (redirects and `304` included) |
|
|
12
|
+
| A server route, or a page marked `prerender: false` | Rendered on request, as before |
|
|
13
|
+
| A page path the build did not write (`/products/nope`) | `404` with the site's `app/error.vue`, no API reads |
|
|
14
|
+
| Any other path (`/.env`, `/wp-login.php`, `/.git/config`) | Bare `404`, no render |
|
|
15
|
+
| A `POST`, `PUT` or other write to a path that is not live | `405` |
|
|
16
|
+
|
|
17
|
+
So a crawler guessing addresses costs the API nothing, and a page published in Studio exists once
|
|
18
|
+
the build that publish starts has finished.
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
pnpm add @eldrajs/nuxt
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
// nuxt.config.ts
|
|
26
|
+
export default defineNuxtConfig({
|
|
27
|
+
modules: ['@eldrajs/nuxt'],
|
|
28
|
+
nitro: { preset: 'cloudflare_module', prerender: { routes: ['/'] } },
|
|
29
|
+
routeRules: { '/coming-soon': { prerender: false } },
|
|
30
|
+
});
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
```jsonc
|
|
34
|
+
// wrangler.jsonc: the Worker runs first, so it can answer before the asset store's own 404.
|
|
35
|
+
"assets": { "directory": "./.output/public", "binding": "ASSETS", "run_worker_first": ["/*", "!/_nuxt/*"] }
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## What it needs from the site
|
|
39
|
+
|
|
40
|
+
- **`app/error.vue` reads nothing from Eldra.** It is the 404 page for every unbuilt page path.
|
|
41
|
+
Give it its own plain header instead of the site's, whose menu comes from Studio.
|
|
42
|
+
- **App plugins skip their reads on a 404.** A plugin that reads on the server checks
|
|
43
|
+
`isUnbuiltPage()` (auto-imported) first:
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
if (import.meta.server && !isUnbuiltPage()) settings.value = await loadSettings();
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
- **Live routes are found, not listed.** Every server route (`server/api`, `server/routes`, a
|
|
50
|
+
module's) and every `routeRules` entry with `prerender: false` is rendered on request. Anything
|
|
51
|
+
else rendered on request goes in `live`.
|
|
52
|
+
|
|
53
|
+
## Options (`eldraNuxt`)
|
|
54
|
+
|
|
55
|
+
| Option | Default | Meaning |
|
|
56
|
+
| ---------------- | ------- | ----------------------------------------------------------------------------- |
|
|
57
|
+
| `builtPagesOnly` | `true` | `false` renders every unbuilt path on request (the Nuxt default). |
|
|
58
|
+
| `live` | `[]` | More route patterns rendered on request without a built file (`/preview/**`). |
|
|
59
|
+
|
|
60
|
+
It changes nothing in `nuxi dev`, under a Nitro preset that is not a Cloudflare one, or on a build
|
|
61
|
+
that prerendered no page (an SSR-only site): the build logs why, and every page is rendered as
|
|
62
|
+
before. Pin the preset in `nuxt.config.ts`; an auto-detected one is not seen.
|
|
63
|
+
|
|
64
|
+
Nuxt renders an error page through an internal `/__nuxt_error` request. A visitor's own request
|
|
65
|
+
to that path is answered with a bare 404, since it would otherwise render on request.
|
|
66
|
+
|
|
67
|
+
## Server helper
|
|
68
|
+
|
|
69
|
+
`fetchBuiltFile(event, path?)` (auto-imported in server code) returns the built file for a path,
|
|
70
|
+
or `null`. Headers set on the event by earlier middleware (a password gate's `cache-control`) are
|
|
71
|
+
kept. Site middleware runs before this module's, so a gate stays in front of the built files.
|
package/dist/module.cjs
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { NuxtModule } from '@nuxt/schema';
|
|
2
|
+
|
|
3
|
+
interface ModuleOptions {
|
|
4
|
+
/** Serve only what the build wrote and answer 404 for the rest. Default `true`. */
|
|
5
|
+
builtPagesOnly: boolean;
|
|
6
|
+
/**
|
|
7
|
+
* More paths the server renders on request, as route patterns (`/preview/**`). Server routes and
|
|
8
|
+
* pages marked `prerender: false` in `routeRules` are found without being listed.
|
|
9
|
+
*/
|
|
10
|
+
live: string[];
|
|
11
|
+
}
|
|
12
|
+
declare const module: NuxtModule<ModuleOptions>;
|
|
13
|
+
|
|
14
|
+
export { module as default };
|
|
15
|
+
export type { ModuleOptions };
|
package/dist/module.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { NuxtModule } from '@nuxt/schema';
|
|
2
|
+
|
|
3
|
+
interface ModuleOptions {
|
|
4
|
+
/** Serve only what the build wrote and answer 404 for the rest. Default `true`. */
|
|
5
|
+
builtPagesOnly: boolean;
|
|
6
|
+
/**
|
|
7
|
+
* More paths the server renders on request, as route patterns (`/preview/**`). Server routes and
|
|
8
|
+
* pages marked `prerender: false` in `routeRules` are found without being listed.
|
|
9
|
+
*/
|
|
10
|
+
live: string[];
|
|
11
|
+
}
|
|
12
|
+
declare const module: NuxtModule<ModuleOptions>;
|
|
13
|
+
|
|
14
|
+
export { module as default };
|
|
15
|
+
export type { ModuleOptions };
|
package/dist/module.json
ADDED
package/dist/module.mjs
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { defineNuxtModule, createResolver, addImports, addServerImports, useLogger, addRouteMiddleware, addServerHandler } from '@nuxt/kit';
|
|
2
|
+
|
|
3
|
+
function notPrerenderedPages(...rules) {
|
|
4
|
+
return rules.flatMap(
|
|
5
|
+
(table) => Object.entries(table ?? {}).filter(([, rule]) => rule?.prerender === false).map(([route]) => route)
|
|
6
|
+
);
|
|
7
|
+
}
|
|
8
|
+
function livePatterns(input) {
|
|
9
|
+
const routes = input.handlers.filter((handler) => handler.route && !handler.middleware).map((handler) => handler.route);
|
|
10
|
+
return [.../* @__PURE__ */ new Set([...input.extra, ...notPrerenderedPages(...input.routeRules), ...routes])];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const VIRTUAL = "#eldra-built-pages";
|
|
14
|
+
const module = defineNuxtModule({
|
|
15
|
+
meta: {
|
|
16
|
+
name: "@eldrajs/nuxt",
|
|
17
|
+
configKey: "eldraNuxt",
|
|
18
|
+
compatibility: { nuxt: ">=3.13.0" }
|
|
19
|
+
},
|
|
20
|
+
defaults: { builtPagesOnly: true, live: [] },
|
|
21
|
+
setup(options, nuxt) {
|
|
22
|
+
const resolver = createResolver(import.meta.url);
|
|
23
|
+
addImports([{ name: "isUnbuiltPage", from: resolver.resolve("./runtime/unbuilt") }]);
|
|
24
|
+
addServerImports([
|
|
25
|
+
{ name: "fetchBuiltFile", from: resolver.resolve("./runtime/server/utils") }
|
|
26
|
+
]);
|
|
27
|
+
if (!options.builtPagesOnly)
|
|
28
|
+
return;
|
|
29
|
+
const logger = useLogger("@eldrajs/nuxt");
|
|
30
|
+
const preset = String(
|
|
31
|
+
process.env.NITRO_PRESET || nuxt.options.nitro?.preset || ""
|
|
32
|
+
);
|
|
33
|
+
if (!/^cloudflare/.test(preset)) {
|
|
34
|
+
logger.info(
|
|
35
|
+
`Built pages only is for Cloudflare presets; "${preset || "none set"}" renders as before.`
|
|
36
|
+
);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
const state = { pages: 0 };
|
|
40
|
+
const hook = nuxt.hook;
|
|
41
|
+
addRouteMiddleware(
|
|
42
|
+
{ name: "eldra-built-pages", path: resolver.resolve("./runtime/middleware"), global: true },
|
|
43
|
+
{ prepend: true }
|
|
44
|
+
);
|
|
45
|
+
addServerHandler({
|
|
46
|
+
middleware: true,
|
|
47
|
+
handler: resolver.resolve("./runtime/server/middleware")
|
|
48
|
+
});
|
|
49
|
+
nuxt.hook("modules:done", () => {
|
|
50
|
+
const nuxtOptions = nuxt.options;
|
|
51
|
+
const pub = nuxt.options.runtimeConfig.public;
|
|
52
|
+
pub.eldraBuiltPages = {
|
|
53
|
+
live: [
|
|
54
|
+
...options.live,
|
|
55
|
+
...notPrerenderedPages(nuxtOptions.routeRules, nuxtOptions.nitro?.routeRules)
|
|
56
|
+
]
|
|
57
|
+
};
|
|
58
|
+
});
|
|
59
|
+
hook("nitro:config", (config) => {
|
|
60
|
+
config.virtual ??= {};
|
|
61
|
+
config.virtual[VIRTUAL] = () => `export default ${JSON.stringify({
|
|
62
|
+
enabled: state.pages > 0,
|
|
63
|
+
live: state.nitro ? livePatterns({
|
|
64
|
+
handlers: [...state.nitro.scannedHandlers, ...state.nitro.options.handlers],
|
|
65
|
+
routeRules: [state.nitro.options.routeRules],
|
|
66
|
+
extra: options.live
|
|
67
|
+
}) : options.live
|
|
68
|
+
})}`;
|
|
69
|
+
});
|
|
70
|
+
hook("nitro:init", (nitro) => {
|
|
71
|
+
state.nitro = nitro;
|
|
72
|
+
nitro.hooks.hook("prerender:route", (route) => {
|
|
73
|
+
if (!route.error && route.contentType?.startsWith("text/html"))
|
|
74
|
+
state.pages++;
|
|
75
|
+
});
|
|
76
|
+
nitro.hooks.hook("prerender:done", () => {
|
|
77
|
+
if (state.pages === 0)
|
|
78
|
+
logger.warn("No page was prerendered: every page is rendered on request.");
|
|
79
|
+
else
|
|
80
|
+
logger.info(`${state.pages} built pages; any other page path answers 404.`);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
nuxt.hook("prepare:types", ({ references }) => {
|
|
84
|
+
references.push({ path: resolver.resolve("./runtime/virtual.d.ts") });
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
export { module as default };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import {
|
|
2
|
+
abortNavigation,
|
|
3
|
+
createError,
|
|
4
|
+
defineNuxtRouteMiddleware,
|
|
5
|
+
getAppManifest,
|
|
6
|
+
isPrerendered,
|
|
7
|
+
navigateTo,
|
|
8
|
+
useNuxtApp,
|
|
9
|
+
useRequestEvent,
|
|
10
|
+
useRuntimeConfig
|
|
11
|
+
} from "nuxt/app";
|
|
12
|
+
import { createLiveMatcher } from "./paths.js";
|
|
13
|
+
import { UNBUILT_ERROR_DATA } from "./unbuilt.js";
|
|
14
|
+
let isLive;
|
|
15
|
+
export default defineNuxtRouteMiddleware(async (to) => {
|
|
16
|
+
if (import.meta.server) {
|
|
17
|
+
if (!useRequestEvent()?.context.eldraUnbuilt) return;
|
|
18
|
+
return abortNavigation(
|
|
19
|
+
createError({ statusCode: 404, statusMessage: "Page not found", data: UNBUILT_ERROR_DATA })
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
if (import.meta.dev || useNuxtApp().isHydrating) return;
|
|
23
|
+
const config = useRuntimeConfig().public.eldraBuiltPages;
|
|
24
|
+
isLive ??= createLiveMatcher(config?.live ?? []);
|
|
25
|
+
if (isLive(to.path)) return;
|
|
26
|
+
const manifest = await getAppManifest().catch(() => null);
|
|
27
|
+
if (!manifest?.prerendered.length) return;
|
|
28
|
+
if (!await isPrerendered(to.path)) return navigateTo(to.fullPath, { external: true });
|
|
29
|
+
});
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** What a path with no built file is: a page gets the site's 404 page, anything else a bare 404. */
|
|
2
|
+
export declare function isPagePath(path: string): boolean;
|
|
3
|
+
/** Matches route patterns in Nitro's syntax (`/api/**`, `/products/:slug`, `/coming-soon`). */
|
|
4
|
+
export declare function createLiveMatcher(patterns: readonly string[]): (path: string) => boolean;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { createRouter, toRouteMatcher } from "radix3";
|
|
2
|
+
export function isPagePath(path) {
|
|
3
|
+
if (/\/\./.test(path)) return false;
|
|
4
|
+
const last = path.slice(path.lastIndexOf("/") + 1);
|
|
5
|
+
return !/\.[a-z0-9]+$/i.test(last);
|
|
6
|
+
}
|
|
7
|
+
export function createLiveMatcher(patterns) {
|
|
8
|
+
if (!patterns.length) return () => false;
|
|
9
|
+
const router = createRouter({ routes: Object.fromEntries(patterns.map((route) => [route, {}])) });
|
|
10
|
+
const rules = toRouteMatcher(router);
|
|
11
|
+
return (path) => {
|
|
12
|
+
const pathname = path.split("?")[0] || "/";
|
|
13
|
+
return router.lookup(pathname) !== null || rules.matchAll(pathname).length > 0;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export interface AssetsBinding {
|
|
2
|
+
fetch(request: Request): Promise<Response>;
|
|
3
|
+
}
|
|
4
|
+
export type BuiltDecision = {
|
|
5
|
+
kind: 'built';
|
|
6
|
+
response: Response;
|
|
7
|
+
} | {
|
|
8
|
+
kind: 'live';
|
|
9
|
+
} | {
|
|
10
|
+
kind: 'unbuilt-page';
|
|
11
|
+
} | {
|
|
12
|
+
kind: 'not-found';
|
|
13
|
+
} | {
|
|
14
|
+
kind: 'method-not-allowed';
|
|
15
|
+
};
|
|
16
|
+
export interface BuiltRequest {
|
|
17
|
+
method: string;
|
|
18
|
+
url: URL;
|
|
19
|
+
headers: Headers;
|
|
20
|
+
assets: AssetsBinding;
|
|
21
|
+
isLive: (path: string) => boolean;
|
|
22
|
+
}
|
|
23
|
+
/** The built file's response, `null` when the build has none, `undefined` when the store failed. */
|
|
24
|
+
export declare function fetchBuilt(assets: AssetsBinding, url: URL, init: {
|
|
25
|
+
method: string;
|
|
26
|
+
headers: Headers;
|
|
27
|
+
}): Promise<Response | null | undefined>;
|
|
28
|
+
/** The built file if there is one; else the site's own routes; else 404 or 405, before any render. */
|
|
29
|
+
export declare function decideBuilt(request: BuiltRequest): Promise<BuiltDecision>;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { isPagePath } from "../paths.js";
|
|
2
|
+
const READS = /* @__PURE__ */ new Set(["GET", "HEAD"]);
|
|
3
|
+
export async function fetchBuilt(assets, url, init) {
|
|
4
|
+
const response = await assets.fetch(new Request(url, { method: init.method, headers: init.headers })).catch(() => void 0);
|
|
5
|
+
if (!response) return void 0;
|
|
6
|
+
return response.status === 404 ? null : response;
|
|
7
|
+
}
|
|
8
|
+
export async function decideBuilt(request) {
|
|
9
|
+
const { method, url, headers, assets, isLive } = request;
|
|
10
|
+
if (READS.has(method)) {
|
|
11
|
+
const built = await fetchBuilt(assets, url, { method, headers });
|
|
12
|
+
if (built === void 0) return { kind: "live" };
|
|
13
|
+
if (built) return { kind: "built", response: built };
|
|
14
|
+
}
|
|
15
|
+
if (isLive(url.pathname)) return { kind: "live" };
|
|
16
|
+
if (!READS.has(method)) return { kind: "method-not-allowed" };
|
|
17
|
+
return isPagePath(url.pathname) ? { kind: "unbuilt-page" } : { kind: "not-found" };
|
|
18
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { defineEventHandler, getQuery, getRequestHeader, getRequestURL } from "h3";
|
|
2
|
+
import builtPages from "#eldra-built-pages";
|
|
3
|
+
import { createLiveMatcher } from "../paths.js";
|
|
4
|
+
import { decideBuilt } from "./built.js";
|
|
5
|
+
import { assetsOf, withEventHeaders } from "./utils.js";
|
|
6
|
+
const isLive = createLiveMatcher(builtPages.live);
|
|
7
|
+
export default defineEventHandler(async (event) => {
|
|
8
|
+
if (import.meta.dev || !builtPages.enabled) return;
|
|
9
|
+
const url = getRequestURL(event);
|
|
10
|
+
if (url.pathname.startsWith("/__nuxt_error")) {
|
|
11
|
+
if (getRequestHeader(event, "x-nuxt-error") !== "true") return notFound();
|
|
12
|
+
if (String(getQuery(event).data ?? "").includes('"eldraUnbuilt":true'))
|
|
13
|
+
event.context.eldraUnbuiltErrorPage = true;
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
const assets = assetsOf(event);
|
|
17
|
+
if (!assets) return;
|
|
18
|
+
const decision = await decideBuilt({
|
|
19
|
+
method: event.method,
|
|
20
|
+
url,
|
|
21
|
+
headers: event.headers,
|
|
22
|
+
assets,
|
|
23
|
+
isLive
|
|
24
|
+
});
|
|
25
|
+
switch (decision.kind) {
|
|
26
|
+
case "built":
|
|
27
|
+
return withEventHeaders(event, decision.response);
|
|
28
|
+
case "unbuilt-page":
|
|
29
|
+
event.context.eldraUnbuilt = true;
|
|
30
|
+
return;
|
|
31
|
+
case "not-found":
|
|
32
|
+
return notFound();
|
|
33
|
+
case "method-not-allowed":
|
|
34
|
+
return new Response("Method not allowed", {
|
|
35
|
+
status: 405,
|
|
36
|
+
headers: { allow: "GET, HEAD", "content-type": "text/plain; charset=utf-8" }
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
function notFound() {
|
|
41
|
+
return new Response("Not found", {
|
|
42
|
+
status: 404,
|
|
43
|
+
headers: { "content-type": "text/plain; charset=utf-8" }
|
|
44
|
+
});
|
|
45
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { type H3Event } from 'h3';
|
|
2
|
+
import { type AssetsBinding } from './built.js';
|
|
3
|
+
export declare function assetsOf(event: H3Event): AssetsBinding | undefined;
|
|
4
|
+
/** The built file's response, minus any header earlier middleware already set on the event. */
|
|
5
|
+
export declare function withEventHeaders(event: H3Event, response: Response): Response;
|
|
6
|
+
/**
|
|
7
|
+
* The file the build wrote for `path` (default: the request's own), or `null` when there is none,
|
|
8
|
+
* when the asset store cannot be asked, or off Cloudflare.
|
|
9
|
+
*/
|
|
10
|
+
export declare function fetchBuiltFile(event: H3Event, path?: string): Promise<Response | null>;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { getRequestURL, getResponseHeaders } from "h3";
|
|
2
|
+
import { fetchBuilt } from "./built.js";
|
|
3
|
+
export function assetsOf(event) {
|
|
4
|
+
return event.context.cloudflare?.env?.ASSETS;
|
|
5
|
+
}
|
|
6
|
+
export function withEventHeaders(event, response) {
|
|
7
|
+
const headers = new Headers(response.headers);
|
|
8
|
+
for (const name of Object.keys(getResponseHeaders(event))) headers.delete(name);
|
|
9
|
+
return new Response(response.body, {
|
|
10
|
+
status: response.status,
|
|
11
|
+
statusText: response.statusText,
|
|
12
|
+
headers
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
export async function fetchBuiltFile(event, path) {
|
|
16
|
+
const assets = assetsOf(event);
|
|
17
|
+
if (!assets) return null;
|
|
18
|
+
const url = getRequestURL(event);
|
|
19
|
+
if (path) url.pathname = path;
|
|
20
|
+
const built = await fetchBuilt(assets, url, { method: event.method, headers: event.headers });
|
|
21
|
+
return built ? withEventHeaders(event, built) : null;
|
|
22
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { useRequestEvent } from "nuxt/app";
|
|
2
|
+
export const UNBUILT_ERROR_DATA = { eldraUnbuilt: true };
|
|
3
|
+
export function isUnbuiltPage() {
|
|
4
|
+
if (!import.meta.server) return false;
|
|
5
|
+
const context = useRequestEvent()?.context;
|
|
6
|
+
return Boolean(context?.eldraUnbuilt || context?.eldraUnbuiltErrorPage);
|
|
7
|
+
}
|
package/dist/types.d.mts
ADDED
package/dist/types.d.ts
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,56 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@eldrajs/nuxt",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Nuxt module for Eldra storefronts on Cloudflare: serves the pages the build wrote and answers 404 for the rest without rendering or reading the API.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"cloudflare",
|
|
7
|
+
"eldra",
|
|
8
|
+
"headless",
|
|
9
|
+
"nuxt",
|
|
10
|
+
"nuxt-module",
|
|
11
|
+
"storefront"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://github.com/eldra-is/headless-kit/tree/main/packages/nuxt#readme",
|
|
14
|
+
"bugs": "https://github.com/eldra-is/headless-kit/issues",
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/eldra-is/headless-kit.git",
|
|
19
|
+
"directory": "packages/nuxt"
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist",
|
|
23
|
+
"CHANGELOG.md",
|
|
24
|
+
"LICENSE"
|
|
25
|
+
],
|
|
26
|
+
"type": "module",
|
|
27
|
+
"exports": {
|
|
28
|
+
".": {
|
|
29
|
+
"types": "./dist/types.d.mts",
|
|
30
|
+
"import": "./dist/module.mjs"
|
|
31
|
+
},
|
|
32
|
+
"./package.json": "./package.json"
|
|
33
|
+
},
|
|
34
|
+
"publishConfig": {
|
|
35
|
+
"access": "public",
|
|
36
|
+
"provenance": true
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@nuxt/kit": "^4.0.0",
|
|
40
|
+
"h3": "^1.15.0",
|
|
41
|
+
"radix3": "^1.1.2"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@nuxt/module-builder": "^0.8.4",
|
|
45
|
+
"@nuxt/schema": "^4.0.0",
|
|
46
|
+
"nuxt": "^4.0.0"
|
|
47
|
+
},
|
|
48
|
+
"engines": {
|
|
49
|
+
"node": ">=20"
|
|
50
|
+
},
|
|
51
|
+
"scripts": {
|
|
52
|
+
"build": "nuxt-module-build build",
|
|
53
|
+
"typecheck": "tsc -p tsconfig.json",
|
|
54
|
+
"test": "vitest run"
|
|
55
|
+
}
|
|
6
56
|
}
|