@fastly/compute-js-static-publish 2.3.0-alpha.1 → 2.3.0-alpha.4
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/build/build-static.d.ts.map +1 -1
- package/build/build-static.js +1 -0
- package/build/index.d.ts +1 -1
- package/build/index.d.ts.map +1 -1
- package/build/static-assets.d.ts +4 -3
- package/build/static-assets.d.ts.map +1 -1
- package/build/static-assets.js +10 -7
- package/package.json +1 -1
- package/resources/index.js +28 -31
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"build-static.d.ts","sourceRoot":"","sources":["../src/build-static.ts"],"names":[],"mappings":"AAcA,OAAO,eAAe,MAAM,mBAAmB,CAAC;AAchD,wBAAsB,iBAAiB,CAAC,iBAAiB,EAAE,eAAe,CAAC,kBAAkB,
|
|
1
|
+
{"version":3,"file":"build-static.d.ts","sourceRoot":"","sources":["../src/build-static.ts"],"names":[],"mappings":"AAcA,OAAO,eAAe,MAAM,mBAAmB,CAAC;AAchD,wBAAsB,iBAAiB,CAAC,iBAAiB,EAAE,eAAe,CAAC,kBAAkB,iBAwN5F"}
|
package/build/build-static.js
CHANGED
|
@@ -132,6 +132,7 @@ export async function buildStaticLoader(commandLineValues) {
|
|
|
132
132
|
}
|
|
133
133
|
knownAssets[filePath] = { contentType: type, isStatic, loadModule, };
|
|
134
134
|
}
|
|
135
|
+
fileContents += 'import { StaticAssets } from "@fastly/compute-js-static-publish";\n';
|
|
135
136
|
fileContents += `\nexport const assets = {\n`;
|
|
136
137
|
for (const [index, file] of files.entries()) {
|
|
137
138
|
const contentDef = defaultContentTypes.testFileContentType(finalContentTypes, file);
|
package/build/index.d.ts
CHANGED
package/build/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,YAAY,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AACnD,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC"}
|
package/build/static-assets.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
/// <reference types="@fastly/js-compute" />
|
|
2
|
-
import { AssetsMap } from "./types.js";
|
|
2
|
+
import { Asset, AssetsMap } from "./types.js";
|
|
3
3
|
export declare class StaticAssets {
|
|
4
4
|
readonly assetsMap: AssetsMap;
|
|
5
5
|
constructor(assetsMap: AssetsMap);
|
|
6
|
-
getAsset(key: string):
|
|
7
|
-
serveAsset(
|
|
6
|
+
getAsset(key: string): Asset;
|
|
7
|
+
serveAsset(asset: Asset): Response;
|
|
8
|
+
serveAssetForEvent(event: FetchEvent, pathPrefix?: string): Response | null;
|
|
8
9
|
}
|
|
9
10
|
//# sourceMappingURL=static-assets.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"static-assets.d.ts","sourceRoot":"","sources":["../src/static-assets.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"static-assets.d.ts","sourceRoot":"","sources":["../src/static-assets.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAE9C,qBAAa,YAAY;IACvB,QAAQ,CAAC,SAAS,EAAE,SAAS,CAAC;gBAElB,SAAS,EAAE,SAAS;IAIzB,QAAQ,CAAC,GAAG,EAAE,MAAM,GAAG,KAAK;IAI5B,UAAU,CAAC,KAAK,EAAE,KAAK;IAcvB,kBAAkB,CAAC,KAAK,EAAE,UAAU,EAAE,UAAU,GAAE,MAAW;CAWrE"}
|
package/build/static-assets.js
CHANGED
|
@@ -5,13 +5,7 @@ export class StaticAssets {
|
|
|
5
5
|
getAsset(key) {
|
|
6
6
|
return this.assetsMap[key];
|
|
7
7
|
}
|
|
8
|
-
serveAsset(
|
|
9
|
-
const { request } = event;
|
|
10
|
-
const { pathname } = new URL(request.url);
|
|
11
|
-
const asset = this.getAsset(`${pathPrefix}${pathname}`);
|
|
12
|
-
if (!asset) {
|
|
13
|
-
return null;
|
|
14
|
-
}
|
|
8
|
+
serveAsset(asset) {
|
|
15
9
|
// Aggressive caching for static files, and no caching for everything else.
|
|
16
10
|
const headers = {
|
|
17
11
|
'Cache-Control': asset.isStatic ? 'max-age=31536000' : 'no-cache',
|
|
@@ -24,4 +18,13 @@ export class StaticAssets {
|
|
|
24
18
|
headers,
|
|
25
19
|
});
|
|
26
20
|
}
|
|
21
|
+
serveAssetForEvent(event, pathPrefix = '') {
|
|
22
|
+
const { request } = event;
|
|
23
|
+
const { pathname } = new URL(request.url);
|
|
24
|
+
const asset = this.getAsset(`${pathPrefix}${pathname}`);
|
|
25
|
+
if (!asset) {
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
return this.serveAsset(asset);
|
|
29
|
+
}
|
|
27
30
|
}
|
package/package.json
CHANGED
package/resources/index.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="@fastly/js-compute" />
|
|
2
2
|
|
|
3
3
|
import { Router } from '@fastly/expressly';
|
|
4
|
-
import {
|
|
4
|
+
import { staticAssets, spaFile, notFoundPageFile, autoIndex, autoExt } from './statics';
|
|
5
5
|
|
|
6
6
|
const router = new Router();
|
|
7
7
|
|
|
@@ -10,7 +10,7 @@ function getMatchingRequestPath(path) {
|
|
|
10
10
|
// match exactly one of the assets
|
|
11
11
|
if(!path.endsWith('/')) {
|
|
12
12
|
|
|
13
|
-
if(path
|
|
13
|
+
if (staticAssets.getAsset(path) != null) {
|
|
14
14
|
return path;
|
|
15
15
|
}
|
|
16
16
|
|
|
@@ -18,7 +18,7 @@ function getMatchingRequestPath(path) {
|
|
|
18
18
|
if(autoExt != null) {
|
|
19
19
|
for (const extEntry of autoExt) {
|
|
20
20
|
let pathWithExt = path + extEntry;
|
|
21
|
-
if(pathWithExt
|
|
21
|
+
if (staticAssets.getAsset(pathWithExt) != null) {
|
|
22
22
|
return pathWithExt;
|
|
23
23
|
}
|
|
24
24
|
}
|
|
@@ -32,7 +32,7 @@ function getMatchingRequestPath(path) {
|
|
|
32
32
|
if(autoIndex != null) {
|
|
33
33
|
for (const indexEntry of autoIndex) {
|
|
34
34
|
let indexPath = path + indexEntry;
|
|
35
|
-
if(indexPath
|
|
35
|
+
if (staticAssets.getAsset(indexPath) != null) {
|
|
36
36
|
return indexPath;
|
|
37
37
|
}
|
|
38
38
|
}
|
|
@@ -53,23 +53,13 @@ function requestAcceptsTextHtml(req) {
|
|
|
53
53
|
|
|
54
54
|
router.get("*", (req, res) => {
|
|
55
55
|
const assetPath = getMatchingRequestPath(req.urlObj.pathname);
|
|
56
|
-
|
|
56
|
+
const asset = staticAssets.getAsset(assetPath);
|
|
57
|
+
if(asset == null) {
|
|
57
58
|
return;
|
|
58
59
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
// https://create-react-app.dev/docs/production-build/#static-file-caching
|
|
63
|
-
const headers = {
|
|
64
|
-
'Cache-Control': staticFile.isStatic ? 'max-age=31536000' : 'no-cache',
|
|
65
|
-
};
|
|
66
|
-
if(staticFile.contentType != null) {
|
|
67
|
-
headers['Content-Type'] = staticFile.contentType;
|
|
68
|
-
}
|
|
69
|
-
res.send(new Response(staticFile.content, {
|
|
70
|
-
status: 200,
|
|
71
|
-
headers,
|
|
72
|
-
}));
|
|
60
|
+
|
|
61
|
+
const response = staticAssets.serveAsset(asset);
|
|
62
|
+
res.send(response);
|
|
73
63
|
});
|
|
74
64
|
|
|
75
65
|
// TODO: If you need to handle any API routes, add them here.
|
|
@@ -85,28 +75,35 @@ router.get("*", (req, res) => {
|
|
|
85
75
|
if(!requestAcceptsTextHtml(req)) {
|
|
86
76
|
return;
|
|
87
77
|
}
|
|
78
|
+
const asset = staticAssets.getAsset(spaFile);
|
|
79
|
+
if(asset == null) {
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
88
82
|
|
|
89
|
-
const
|
|
90
|
-
res.send(new Response(staticFile.content, {
|
|
83
|
+
const response = new Response(asset.content, {
|
|
91
84
|
status: 200,
|
|
92
85
|
headers: {
|
|
93
86
|
'Cache-Control': 'no-cache',
|
|
94
87
|
'Content-Type': 'text/html',
|
|
95
88
|
}
|
|
96
|
-
})
|
|
89
|
+
});
|
|
90
|
+
res.send(response);
|
|
97
91
|
});
|
|
98
92
|
|
|
99
93
|
router.all("*", (req, res) => {
|
|
100
94
|
if(notFoundPageFile && requestAcceptsTextHtml(req)) {
|
|
101
|
-
const
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
95
|
+
const asset = staticAssets.getAsset(notFoundPageFile);
|
|
96
|
+
if(asset != null) {
|
|
97
|
+
const response = new Response(asset.content, {
|
|
98
|
+
status: 404,
|
|
99
|
+
headers: {
|
|
100
|
+
'Cache-Control': 'no-cache',
|
|
101
|
+
'Content-Type': 'text/html',
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
res.send(response);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
110
107
|
}
|
|
111
108
|
|
|
112
109
|
res.send(new Response("404 Not Found", {
|