@pracht/cli 1.1.3 → 1.1.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/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # @pracht/cli
2
2
 
3
+ ## 1.1.4
4
+
5
+ ### Patch Changes
6
+
7
+ - [#88](https://github.com/JoviDeCroock/pracht/pull/88) [`f36f102`](https://github.com/JoviDeCroock/pracht/commit/f36f102eb9494ec8ea1db3fe20219ad95ccab257) Thanks [@kinngh](https://github.com/kinngh)! - Add shell and route `headers()` exports for page document responses. Headers merge like `head()` metadata, are preserved in prerender output, and are applied to static SSG/ISG HTML served by the built-in adapters.
8
+
9
+ - Updated dependencies [[`f36f102`](https://github.com/JoviDeCroock/pracht/commit/f36f102eb9494ec8ea1db3fe20219ad95ccab257)]:
10
+ - @pracht/core@0.2.4
11
+
3
12
  ## 1.1.3
4
13
 
5
14
  ### Patch Changes
@@ -14,7 +14,14 @@ export function setDefaultSecurityHeaders(res, headers = {}) {
14
14
  }
15
15
  }
16
16
 
17
- export function writeVercelBuildOutput({ functionName, regions, root, staticRoutes, isgRoutes }) {
17
+ export function writeVercelBuildOutput({
18
+ functionName,
19
+ headersManifest = {},
20
+ regions,
21
+ root,
22
+ staticRoutes,
23
+ isgRoutes,
24
+ }) {
18
25
  const outputDir = join(root, ".vercel/output");
19
26
  const staticDir = join(outputDir, "static");
20
27
  const functionDir = join(outputDir, "functions", `${functionName || "render"}.func`);
@@ -26,7 +33,11 @@ export function writeVercelBuildOutput({ functionName, regions, root, staticRout
26
33
 
27
34
  writeFileSync(
28
35
  join(outputDir, "config.json"),
29
- `${JSON.stringify(createVercelOutputConfig({ functionName, staticRoutes, isgRoutes }), null, 2)}\n`,
36
+ `${JSON.stringify(
37
+ createVercelOutputConfig({ functionName, headersManifest, staticRoutes, isgRoutes }),
38
+ null,
39
+ 2,
40
+ )}\n`,
30
41
  "utf-8",
31
42
  );
32
43
  writeFileSync(
@@ -38,7 +49,7 @@ export function writeVercelBuildOutput({ functionName, regions, root, staticRout
38
49
  return ".vercel/output";
39
50
  }
40
51
 
41
- function createVercelOutputConfig({ functionName, staticRoutes, isgRoutes }) {
52
+ function createVercelOutputConfig({ functionName, headersManifest, staticRoutes, isgRoutes }) {
42
53
  const target = `/${functionName || "render"}`;
43
54
  const routes = [
44
55
  {
@@ -65,22 +76,33 @@ function createVercelOutputConfig({ functionName, staticRoutes, isgRoutes }) {
65
76
  routes.push({ handle: "filesystem" });
66
77
  routes.push({ dest: target, src: "/(.*)" });
67
78
 
79
+ const headers = [
80
+ {
81
+ headers: [
82
+ {
83
+ key: "permissions-policy",
84
+ value:
85
+ "accelerometer=(), camera=(), geolocation=(), gyroscope=(), magnetometer=(), microphone=(), payment=(), usb=()",
86
+ },
87
+ { key: "referrer-policy", value: "strict-origin-when-cross-origin" },
88
+ { key: "x-content-type-options", value: "nosniff" },
89
+ { key: "x-frame-options", value: "SAMEORIGIN" },
90
+ ],
91
+ source: "/(.*)",
92
+ },
93
+ ];
94
+
95
+ for (const route of sortStaticRoutes(staticRoutes)) {
96
+ const routeHeaders = headersManifest[route];
97
+ if (!routeHeaders) continue;
98
+ headers.push({
99
+ headers: Object.entries(routeHeaders).map(([key, value]) => ({ key, value })),
100
+ source: routeToHeaderSource(route),
101
+ });
102
+ }
103
+
68
104
  return {
69
- headers: [
70
- {
71
- headers: [
72
- {
73
- key: "permissions-policy",
74
- value:
75
- "accelerometer=(), camera=(), geolocation=(), gyroscope=(), magnetometer=(), microphone=(), payment=(), usb=()",
76
- },
77
- { key: "referrer-policy", value: "strict-origin-when-cross-origin" },
78
- { key: "x-content-type-options", value: "nosniff" },
79
- { key: "x-frame-options", value: "SAMEORIGIN" },
80
- ],
81
- source: "/(.*)",
82
- },
83
- ],
105
+ headers,
84
106
  framework: {
85
107
  version: VERSION,
86
108
  },
@@ -122,6 +144,10 @@ function routeToStaticHtmlPath(route) {
122
144
  return `${route}/index.html`;
123
145
  }
124
146
 
147
+ function routeToHeaderSource(route) {
148
+ return route === "/" ? "/" : route;
149
+ }
150
+
125
151
  function escapeRegex(value) {
126
152
  return value.replace(/[|\\{}()[\]^$+*?.-]/g, "\\$&");
127
153
  }
@@ -60,6 +60,9 @@ export async function buildCommand() {
60
60
  registry: serverMod.registry,
61
61
  withISGManifest: true,
62
62
  });
63
+ const headersManifest = Object.fromEntries(
64
+ pages.map((page) => [page.path, page.headers ?? {}]),
65
+ );
63
66
 
64
67
  if (pages.length > 0) {
65
68
  console.log(`\n Prerendering ${pages.length} SSG/ISG route(s)...\n`);
@@ -75,6 +78,17 @@ export async function buildCommand() {
75
78
  }
76
79
  }
77
80
 
81
+ if (Object.keys(headersManifest).length > 0) {
82
+ const headersManifestJson = `${JSON.stringify(headersManifest, null, 2)}\n`;
83
+ writeFileSync(
84
+ resolve(root, "dist/server/headers-manifest.json"),
85
+ headersManifestJson,
86
+ "utf-8",
87
+ );
88
+ mkdirSync(resolve(clientDir, "_pracht"), { recursive: true });
89
+ writeFileSync(resolve(clientDir, "_pracht/headers.json"), headersManifestJson, "utf-8");
90
+ }
91
+
78
92
  if (Object.keys(isgManifest).length > 0) {
79
93
  const isgManifestPath = resolve(root, "dist/server/isg-manifest.json");
80
94
  writeFileSync(isgManifestPath, JSON.stringify(isgManifest, null, 2), "utf-8");
@@ -92,6 +106,7 @@ export async function buildCommand() {
92
106
  const outputPath = writeVercelBuildOutput({
93
107
  functionName: serverMod.vercelFunctionName,
94
108
  isgRoutes: Object.keys(isgManifest),
109
+ headersManifest,
95
110
  regions: serverMod.vercelRegions,
96
111
  root,
97
112
  staticRoutes: pages.map((page) => page.path).filter((path) => !(path in isgManifest)),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pracht/cli",
3
- "version": "1.1.3",
3
+ "version": "1.1.4",
4
4
  "homepage": "https://github.com/JoviDeCroock/pracht/tree/main/packages/cli",
5
5
  "bugs": {
6
6
  "url": "https://github.com/JoviDeCroock/pracht/issues"
@@ -18,6 +18,6 @@
18
18
  "provenance": true
19
19
  },
20
20
  "dependencies": {
21
- "@pracht/core": "0.2.3"
21
+ "@pracht/core": "0.2.4"
22
22
  }
23
23
  }