@fastly/remix-server-adapter 1.0.0 → 2.0.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/README.md
CHANGED
|
@@ -1,41 +1,56 @@
|
|
|
1
1
|
# Remix Adapter for Fastly Compute@Edge
|
|
2
2
|
|
|
3
|
-
An adapter that allows the Compute@Edge JavaScript entry point program to start Remix.
|
|
4
|
-
|
|
5
|
-
[`@fastly/compute-js-static-publish`](https://github.com/fastly/compute-js-static-publish)
|
|
3
|
+
An adapter that allows the Compute@Edge JavaScript entry point program to start Remix. This adapter
|
|
4
|
+
package we have created is designed to be used with Fastly Compute@Edge, and currently uses
|
|
5
|
+
[`@fastly/compute-js-static-publish`](https://github.com/fastly/compute-js-static-publish)
|
|
6
|
+
behind the scenes to include resources into the Wasm package bundle.
|
|
6
7
|
|
|
7
8
|
(`@fastly/compute-js-static-publish` is set up automatically for you if you set up your Remix
|
|
8
9
|
project using [`remix-template`](/packages/remix-template).)
|
|
9
10
|
|
|
10
11
|
## Usage
|
|
11
12
|
|
|
12
|
-
The simplest usage is the `createEventHandler` function
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
The simplest usage is the `createEventHandler` function.
|
|
14
|
+
This function needs to be passed the following parameters:
|
|
15
|
+
|
|
16
|
+
- `build`, obtained by loading `/build/index.js`
|
|
17
|
+
- `server`, obtained by calling `getServer()`, exported from `./statics`
|
|
18
|
+
|
|
19
|
+
> HINT: `./statics` is generated automatically by `@fastly/compute-js-static-publish`.
|
|
15
20
|
|
|
16
21
|
```js
|
|
17
22
|
/// <reference types="@fastly/js-compute" />
|
|
18
|
-
import { createEventHandler } from '@fastly/remix-server-adapter';
|
|
19
|
-
import {
|
|
20
|
-
|
|
21
|
-
|
|
23
|
+
import { createEventHandler } from '@fastly/remix-server-adapter';
|
|
24
|
+
import { moduleAssets, getServer } from './statics';
|
|
25
|
+
|
|
26
|
+
/** @type {import('@remix-run/server-runtime').ServerBuild} */
|
|
27
|
+
const build = moduleAssets.getAsset('/build/index.js').getStaticModule();
|
|
28
|
+
|
|
29
|
+
/** @type {import('@fastly/compute-js-static-publish').PublisherServer} */
|
|
30
|
+
const server = getServer();
|
|
31
|
+
|
|
32
|
+
addEventListener("fetch", createEventHandler({ build, server }));
|
|
22
33
|
```
|
|
23
34
|
|
|
24
|
-
If you need more granular control over the `ServerBuild` module to use with Remix, or whether to handle static assets,
|
|
25
|
-
you may use the lower-level `
|
|
35
|
+
If you need more granular control over the `ServerBuild` module to use with Remix, or whether/how to handle static assets,
|
|
36
|
+
you may use the lower-level `createRequestHandler` and `handleAsset` functions:
|
|
26
37
|
|
|
27
38
|
```js
|
|
28
39
|
/// <reference types="@fastly/js-compute" />
|
|
29
40
|
import { createRequestHandler, handleAsset } from '@fastly/remix-server-adapter';
|
|
30
|
-
import {
|
|
41
|
+
import { moduleAssets, getServer } from './statics';
|
|
42
|
+
|
|
43
|
+
/** @type {import('@remix-run/server-runtime').ServerBuild} */
|
|
44
|
+
const build = moduleAssets.getAsset('/build/index.js').getStaticModule();
|
|
45
|
+
|
|
46
|
+
/** @type {import('@fastly/compute-js-static-publish').PublisherServer} */
|
|
47
|
+
const server = getServer();
|
|
31
48
|
|
|
32
|
-
/** @type {import('@remix-run/server-runtime').ServerBuild} */
|
|
33
|
-
const build = staticAssets.getAsset('/build/index.js').module;
|
|
34
49
|
const requestHandler = createRequestHandler({build});
|
|
35
50
|
|
|
36
51
|
addEventListener("fetch", (event) => event.respondWith(handleRequest(event)));
|
|
37
52
|
async function handleRequest(event) {
|
|
38
|
-
let response = await handleAsset(event,
|
|
53
|
+
let response = await handleAsset(event, build, server);
|
|
39
54
|
|
|
40
55
|
if (!response) {
|
|
41
56
|
response = requestHandler(event);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference types="@fastly/js-compute" />
|
|
2
|
-
import type {
|
|
2
|
+
import type { PublisherServer } from "@fastly/compute-js-static-publish";
|
|
3
3
|
import type { AppLoadContext, ServerBuild } from "@fastly/remix-server-runtime";
|
|
4
4
|
/**
|
|
5
5
|
* A function that returns the value to use as `context` in route `loader` and
|
|
@@ -14,9 +14,10 @@ export type RequestHandler = ReturnType<typeof createRequestHandler>;
|
|
|
14
14
|
* Generates a Response that would serve a static asset corresponding to the URL requested
|
|
15
15
|
* by the passed-in FetchEvent.
|
|
16
16
|
* @param event { FetchEvent }
|
|
17
|
-
* @param
|
|
17
|
+
* @param build { ServerBuild }
|
|
18
|
+
* @param server { PublisherServer }
|
|
18
19
|
*/
|
|
19
|
-
export declare function handleAsset(event: FetchEvent,
|
|
20
|
+
export declare function handleAsset(event: FetchEvent, build: ServerBuild, server: PublisherServer): Promise<Response | null>;
|
|
20
21
|
/**
|
|
21
22
|
* Returns a request handler for the Fastly Compute@Edge runtime that serves the
|
|
22
23
|
* Remix SSR response.
|
|
@@ -28,13 +29,15 @@ export declare function createRequestHandler({ build, getLoadContext, mode, }: {
|
|
|
28
29
|
}): (event: FetchEvent) => Promise<Response>;
|
|
29
30
|
/**
|
|
30
31
|
* Creates a simplified event handler that can be used on Fastly Compute@Edge.
|
|
31
|
-
* @param
|
|
32
|
+
* @param build { ServerBuild }
|
|
32
33
|
* @param getLoadContext { GetLoadContextFunction }
|
|
33
|
-
* @param
|
|
34
|
+
* @param serve { PublisherServer }
|
|
35
|
+
* @param mode { string }
|
|
34
36
|
*/
|
|
35
|
-
export declare function createEventHandler({
|
|
36
|
-
|
|
37
|
+
export declare function createEventHandler({ build, getLoadContext, server, mode, }: {
|
|
38
|
+
build: ServerBuild;
|
|
37
39
|
getLoadContext?: GetLoadContextFunction;
|
|
40
|
+
server: PublisherServer;
|
|
38
41
|
mode?: string;
|
|
39
42
|
}): (event: FetchEvent) => void;
|
|
40
43
|
//# sourceMappingURL=implementation.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"implementation.d.ts","sourceRoot":"","sources":["../../src/implementation.ts"],"names":[],"mappings":";AAKA,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"implementation.d.ts","sourceRoot":"","sources":["../../src/implementation.ts"],"names":[],"mappings":";AAKA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,mCAAmC,CAAC;AAEzE,OAAO,KAAK,EAAE,cAAc,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;AAGhF;;;;;;GAMG;AACH,MAAM,MAAM,sBAAsB,GAAG,CAAC,KAAK,EAAE,UAAU,KAAK,cAAc,CAAC;AAE3E,MAAM,MAAM,cAAc,GAAG,UAAU,CAAC,OAAO,oBAAoB,CAAC,CAAC;AAErE;;;;;;GAMG;AACH,wBAAsB,WAAW,CAC/B,KAAK,EAAE,UAAU,EACjB,KAAK,EAAE,WAAW,EAClB,MAAM,EAAE,eAAe,GACtB,OAAO,CAAC,QAAQ,GAAG,IAAI,CAAC,CA2B1B;AAED;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,EACnC,KAAK,EACL,cAAc,EACd,IAAI,GACL,EAAE;IACD,KAAK,EAAE,WAAW,CAAC;IACnB,cAAc,CAAC,EAAE,sBAAsB,CAAC;IACxC,IAAI,CAAC,EAAE,MAAM,CAAC;CACf,WAGgB,UAAU,uBAgB1B;AAED;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAAC,EACjC,KAAK,EACL,cAAc,EACd,MAAM,EACN,IAAI,GACL,EAAE;IACD,KAAK,EAAE,WAAW,CAAC;IACnB,cAAc,CAAC,EAAE,sBAAsB,CAAC;IACxC,MAAM,EAAE,eAAe,CAAC;IACxB,IAAI,CAAC,EAAE,MAAM,CAAC;CACf,WAiBgB,UAAU,UAgB1B"}
|
|
@@ -10,10 +10,32 @@ const remix_server_runtime_1 = require("@fastly/remix-server-runtime");
|
|
|
10
10
|
* Generates a Response that would serve a static asset corresponding to the URL requested
|
|
11
11
|
* by the passed-in FetchEvent.
|
|
12
12
|
* @param event { FetchEvent }
|
|
13
|
-
* @param
|
|
13
|
+
* @param build { ServerBuild }
|
|
14
|
+
* @param server { PublisherServer }
|
|
14
15
|
*/
|
|
15
|
-
async function handleAsset(event,
|
|
16
|
-
|
|
16
|
+
async function handleAsset(event, build, server) {
|
|
17
|
+
const request = event.request;
|
|
18
|
+
const requestPathname = new URL(request.url).pathname;
|
|
19
|
+
const asset = server.getMatchingAsset(requestPathname);
|
|
20
|
+
if (asset == null) {
|
|
21
|
+
return null;
|
|
22
|
+
}
|
|
23
|
+
let cache = undefined;
|
|
24
|
+
if (process.env.NODE_ENV === "development") {
|
|
25
|
+
cache = 'never';
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
let assetpath = build.assets.url.split("/").slice(0, -1).join("/");
|
|
29
|
+
let requestpath = requestPathname.split("/").slice(0, -1).join("/");
|
|
30
|
+
if (requestpath.startsWith(assetpath)) {
|
|
31
|
+
// Assets are hashed by Remix so are safe to cache in the browser
|
|
32
|
+
cache = 'extended';
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
// Assets are not necessarily hashed in the request URL, so we cannot cache in the browser
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return server.serveAsset(event.request, asset, { cache });
|
|
17
39
|
}
|
|
18
40
|
exports.handleAsset = handleAsset;
|
|
19
41
|
/**
|
|
@@ -40,19 +62,19 @@ function createRequestHandler({ build, getLoadContext, mode, }) {
|
|
|
40
62
|
exports.createRequestHandler = createRequestHandler;
|
|
41
63
|
/**
|
|
42
64
|
* Creates a simplified event handler that can be used on Fastly Compute@Edge.
|
|
43
|
-
* @param
|
|
65
|
+
* @param build { ServerBuild }
|
|
44
66
|
* @param getLoadContext { GetLoadContextFunction }
|
|
45
|
-
* @param
|
|
67
|
+
* @param serve { PublisherServer }
|
|
68
|
+
* @param mode { string }
|
|
46
69
|
*/
|
|
47
|
-
function createEventHandler({
|
|
48
|
-
const build = staticAssets.getAsset('/build/index.js').module;
|
|
70
|
+
function createEventHandler({ build, getLoadContext, server, mode, }) {
|
|
49
71
|
let handleRequest = createRequestHandler({
|
|
50
72
|
build,
|
|
51
73
|
getLoadContext,
|
|
52
74
|
mode,
|
|
53
75
|
});
|
|
54
76
|
let handleEvent = async (event) => {
|
|
55
|
-
let response = await handleAsset(event,
|
|
77
|
+
let response = await handleAsset(event, build, server);
|
|
56
78
|
if (!response) {
|
|
57
79
|
response = await handleRequest(event);
|
|
58
80
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"implementation.js","sourceRoot":"","sources":["../../src/implementation.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAKH,uEAAiG;AAajG
|
|
1
|
+
{"version":3,"file":"implementation.js","sourceRoot":"","sources":["../../src/implementation.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAKH,uEAAiG;AAajG;;;;;;GAMG;AACI,KAAK,UAAU,WAAW,CAC/B,KAAiB,EACjB,KAAkB,EAClB,MAAuB;IAGvB,MAAM,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC;IAC9B,MAAM,eAAe,GAAG,IAAI,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,QAAQ,CAAC;IAEtD,MAAM,KAAK,GAAG,MAAM,CAAC,gBAAgB,CAAC,eAAe,CAAC,CAAC;IACvD,IAAI,KAAK,IAAI,IAAI,EAAE;QACjB,OAAO,IAAI,CAAC;KACb;IAED,IAAI,KAAK,GAAqC,SAAS,CAAC;IAExD,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,aAAa,EAAE;QAC1C,KAAK,GAAG,OAAO,CAAC;KACjB;SAAM;QACL,IAAI,SAAS,GAAG,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QACnE,IAAI,WAAW,GAAG,eAAe,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAA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,KAAK,EAAE,MAAM,CAAC,CAAC;QAEvD,IAAI,CAAC,QAAQ,EAAE;YACb,QAAQ,GAAG,MAAM,aAAa,CAAC,KAAK,CAAC,CAAC;SACvC;QAED,OAAO,QAAQ,CAAC;IAClB,CAAC,CAAC;IAEF,OAAO,CAAC,KAAiB,EAAE,EAAE;QAC3B,IAAI;YACF,KAAK,CAAC,WAAW,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC,CAAC;SACvC;QAAC,OAAO,CAAM,EAAE;YACf,IAAI,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,aAAa,EAAE;gBAC1C,KAAK,CAAC,WAAW,CACf,IAAI,QAAQ,CAAC,CAAC,CAAC,OAAO,IAAI,CAAC,CAAC,QAAQ,EAAE,EAAE;oBACtC,MAAM,EAAE,GAAG;iBACZ,CAAC,CACH,CAAC;gBACF,OAAO;aACR;YAED,KAAK,CAAC,WAAW,CAAC,IAAI,QAAQ,CAAC,gBAAgB,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC;SACpE;IACH,CAAC,CAAC;AACJ,CAAC;AA3CD,gDA2CC","sourcesContent":["/*\n * Copyright Fastly, Inc.\n * Licensed under the MIT license. See LICENSE file for details.\n */\n\nimport type { PublisherServer } from \"@fastly/compute-js-static-publish\";\n\nimport type { AppLoadContext, ServerBuild } from \"@fastly/remix-server-runtime\";\nimport { createRequestHandler as createRemixRequestHandler } from \"@fastly/remix-server-runtime\";\n\n/**\n * A function that returns the value to use as `context` in route `loader` and\n * `action` functions.\n *\n * You can think of this as an escape hatch that allows you to pass\n * environment/platform-specific values through to your loader/action.\n */\nexport type GetLoadContextFunction = (event: FetchEvent) => AppLoadContext;\n\nexport type RequestHandler = ReturnType<typeof createRequestHandler>;\n\n/**\n * Generates a Response that would serve a static asset corresponding to the URL requested\n * by the passed-in FetchEvent.\n * @param event { FetchEvent }\n * @param build { ServerBuild }\n * @param server { PublisherServer }\n */\nexport async function handleAsset(\n event: FetchEvent,\n build: ServerBuild,\n server: PublisherServer,\n): Promise<Response | null> {\n\n const request = event.request;\n const requestPathname = new URL(request.url).pathname;\n\n const asset = server.getMatchingAsset(requestPathname);\n if (asset == null) {\n return null;\n }\n\n let cache: 'extended' | 'never' | undefined = undefined;\n\n if (process.env.NODE_ENV === \"development\") {\n cache = 'never';\n } else {\n let assetpath = build.assets.url.split(\"/\").slice(0, -1).join(\"/\");\n let requestpath = requestPathname.split(\"/\").slice(0, -1).join(\"/\");\n\n if (requestpath.startsWith(assetpath)) {\n // Assets are hashed by Remix so are safe to cache in the browser\n cache = 'extended';\n } else {\n // Assets are not necessarily hashed in the request URL, so we cannot cache in the browser\n }\n }\n\n return server.serveAsset(event.request, asset, { cache });\n}\n\n/**\n * Returns a request handler for the Fastly Compute@Edge runtime that serves the\n * Remix SSR response.\n */\nexport function createRequestHandler({\n build,\n getLoadContext,\n mode,\n}: {\n build: ServerBuild;\n getLoadContext?: GetLoadContextFunction;\n mode?: string;\n}) {\n let handleRequest = createRemixRequestHandler(build, mode);\n\n return (event: FetchEvent) => {\n let loadContext = getLoadContext?.(event);\n\n // HACK: Until js-compute supports AbortSignal on Request\n // we add a fake AbortSignal that doesn't actually abort\n const request: any = event.request;\n if (request.signal == null) {\n request.signal = {\n aborted: false,\n addEventHandler: () => {},\n removeEventHandler: () => {},\n };\n }\n\n return handleRequest(request, loadContext);\n };\n}\n\n/**\n * Creates a simplified event handler that can be used on Fastly Compute@Edge.\n * @param build { ServerBuild }\n * @param getLoadContext { GetLoadContextFunction }\n * @param serve { PublisherServer }\n * @param mode { string }\n */\nexport function createEventHandler({\n build,\n getLoadContext,\n server,\n mode,\n}: {\n build: ServerBuild;\n getLoadContext?: GetLoadContextFunction;\n server: PublisherServer;\n mode?: string;\n}) {\n let handleRequest = createRequestHandler({\n build,\n getLoadContext,\n mode,\n });\n\n let handleEvent = async (event: FetchEvent) => {\n let response = await handleAsset(event, build, server);\n\n if (!response) {\n response = await handleRequest(event);\n }\n\n return response;\n };\n\n return (event: FetchEvent) => {\n try {\n event.respondWith(handleEvent(event));\n } catch (e: any) {\n if (process.env.NODE_ENV === \"development\") {\n event.respondWith(\n new Response(e.message || e.toString(), {\n status: 500,\n })\n );\n return;\n }\n\n event.respondWith(new Response(\"Internal Error\", { status: 500 }));\n }\n };\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fastly/remix-server-adapter",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Remix Adapter for Fastly Compute@Edge",
|
|
6
6
|
"types": "./build/src/index.d.ts",
|
|
@@ -9,9 +9,9 @@
|
|
|
9
9
|
".": "./build/src/index.js"
|
|
10
10
|
},
|
|
11
11
|
"dependencies": {
|
|
12
|
-
"@fastly/compute-js-static-publish": "^
|
|
13
|
-
"@fastly/js-compute": "^
|
|
14
|
-
"@fastly/remix-server-runtime": "^
|
|
12
|
+
"@fastly/compute-js-static-publish": "^5.0.0",
|
|
13
|
+
"@fastly/js-compute": "^2.0.0",
|
|
14
|
+
"@fastly/remix-server-runtime": "^2.0.0"
|
|
15
15
|
},
|
|
16
16
|
"scripts": {
|
|
17
17
|
"prepack": "npm run clean && npm run compile",
|