vike-lite 1.15.8 → 1.15.9
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/dist/__internal/shared.d.mts +8 -8
- package/dist/__internal/shared.mjs +54 -2
- package/dist/client/router.mjs +2 -4
- package/dist/{server-DSrh6B3f.mjs → server-CRcgzWMc.mjs} +4 -10
- package/dist/server.mjs +1 -1
- package/dist/vite.mjs +4 -5
- package/package.json +1 -1
- package/dist/matchRoute-nTNPxoMq.mjs +0 -44
|
@@ -1,10 +1,4 @@
|
|
|
1
|
-
//#region src/__internal/shared
|
|
2
|
-
declare function matchRoute(urlPathname: string, routes: typeof import('virtual:vike-lite/routes').routes): {
|
|
3
|
-
route: any;
|
|
4
|
-
routeParams: Record<string, string>;
|
|
5
|
-
} | null;
|
|
6
|
-
//#endregion
|
|
7
|
-
//#region src/__internal/shared/index.d.ts
|
|
1
|
+
//#region src/__internal/shared.d.ts
|
|
8
2
|
interface RenderContext {
|
|
9
3
|
pageContext: any;
|
|
10
4
|
Page: unknown;
|
|
@@ -19,5 +13,11 @@ interface RenderContext {
|
|
|
19
13
|
};
|
|
20
14
|
nonce?: string;
|
|
21
15
|
}
|
|
16
|
+
declare function matchRoute(urlPathname: string, routes: typeof import('virtual:vike-lite/routes').routes): {
|
|
17
|
+
route: any;
|
|
18
|
+
routeParams: Record<string, string>;
|
|
19
|
+
} | null;
|
|
20
|
+
declare const BASE_URL: string;
|
|
21
|
+
declare function stripBase(pathname: string): string;
|
|
22
22
|
//#endregion
|
|
23
|
-
export { RenderContext, matchRoute };
|
|
23
|
+
export { BASE_URL, RenderContext, matchRoute, stripBase };
|
|
@@ -1,2 +1,54 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
//#region src/__internal/shared.ts
|
|
2
|
+
const regexCache = /* @__PURE__ */ new Map();
|
|
3
|
+
function escapeRegex(str) {
|
|
4
|
+
return str.replaceAll(/[.*+?^${}()|[\]\\]/g, String.raw`\$&`);
|
|
5
|
+
}
|
|
6
|
+
function matchRoute(urlPathname, routes) {
|
|
7
|
+
for (const route of routes) {
|
|
8
|
+
if (!route.path.includes(":")) {
|
|
9
|
+
if (route.path === urlPathname || route.path + "/" === urlPathname) return {
|
|
10
|
+
route,
|
|
11
|
+
routeParams: {}
|
|
12
|
+
};
|
|
13
|
+
continue;
|
|
14
|
+
}
|
|
15
|
+
let compiled = regexCache.get(route.path);
|
|
16
|
+
if (!compiled) {
|
|
17
|
+
const paramNames = [];
|
|
18
|
+
const regexPath = route.path.split("/").map((segment) => {
|
|
19
|
+
if (segment.startsWith(":")) {
|
|
20
|
+
paramNames.push(segment.slice(1));
|
|
21
|
+
return "([^/]+)";
|
|
22
|
+
}
|
|
23
|
+
return escapeRegex(segment);
|
|
24
|
+
}).join("/");
|
|
25
|
+
compiled = {
|
|
26
|
+
regex: new RegExp(`^${regexPath}/?$`),
|
|
27
|
+
paramNames
|
|
28
|
+
};
|
|
29
|
+
regexCache.set(route.path, compiled);
|
|
30
|
+
}
|
|
31
|
+
const match = urlPathname.match(compiled.regex);
|
|
32
|
+
if (match) {
|
|
33
|
+
const routeParams = {};
|
|
34
|
+
for (let i = 0; i < compiled.paramNames.length; i++) routeParams[compiled.paramNames[i]] = decodeURIComponent(match[i + 1]);
|
|
35
|
+
return {
|
|
36
|
+
route,
|
|
37
|
+
routeParams
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
const BASE_URL = (() => {
|
|
44
|
+
const { BASE_URL } = import.meta.env;
|
|
45
|
+
return BASE_URL.endsWith("/") ? BASE_URL.slice(0, -1) : BASE_URL;
|
|
46
|
+
})();
|
|
47
|
+
function stripBase(pathname) {
|
|
48
|
+
if (BASE_URL === "") return pathname;
|
|
49
|
+
if (pathname === BASE_URL) return "/";
|
|
50
|
+
if (pathname.startsWith(BASE_URL + "/")) return pathname.slice(BASE_URL.length);
|
|
51
|
+
return pathname;
|
|
52
|
+
}
|
|
53
|
+
//#endregion
|
|
54
|
+
export { BASE_URL, matchRoute, stripBase };
|
package/dist/client/router.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { BASE_URL } from "../__internal/shared.mjs";
|
|
1
2
|
//#region src/client/router.ts
|
|
2
3
|
/**
|
|
3
4
|
* Change page programmatically on the client without reloading the browser.
|
|
@@ -7,10 +8,7 @@
|
|
|
7
8
|
function navigate(url, options) {
|
|
8
9
|
if (typeof globalThis === "undefined") throw new Error("navigate() can only be called on the client side.");
|
|
9
10
|
let finalUrl = url;
|
|
10
|
-
if (finalUrl.startsWith("/"))
|
|
11
|
-
const { BASE_URL } = import.meta.env;
|
|
12
|
-
finalUrl = (BASE_URL.endsWith("/") ? BASE_URL.slice(0, -1) : BASE_URL) + (finalUrl === "/" ? "" : finalUrl);
|
|
13
|
-
}
|
|
11
|
+
if (finalUrl.startsWith("/")) finalUrl = BASE_URL + (finalUrl === "/" ? "" : finalUrl);
|
|
14
12
|
globalThis.history.pushState({ triggeredBy: "vike-lite" }, "", finalUrl);
|
|
15
13
|
globalThis.dispatchEvent(new CustomEvent("vike-navigate", { detail: {
|
|
16
14
|
keepScrollPosition: options?.keepScrollPosition,
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { BASE_URL, matchRoute, stripBase } from "./__internal/shared.mjs";
|
|
2
2
|
import { AbortRedirect, AbortRender } from "./server/abort.mjs";
|
|
3
3
|
import { n as store } from "./store-BCs8gL_o.mjs";
|
|
4
4
|
//#region src/utils/serializeContext.ts
|
|
@@ -17,8 +17,7 @@ function serializeContext(data) {
|
|
|
17
17
|
//#region src/server/renderPage.ts
|
|
18
18
|
const isProd = process.env.NODE_ENV === "production";
|
|
19
19
|
function withBase(file) {
|
|
20
|
-
|
|
21
|
-
return `${BASE_URL === "/" ? "" : BASE_URL.endsWith("/") ? BASE_URL.slice(0, -1) : BASE_URL}${file.startsWith("/") ? file : `/${file}`}`;
|
|
20
|
+
return `${BASE_URL === "" ? "" : BASE_URL}${file.startsWith("/") ? file : `/${file}`}`;
|
|
22
21
|
}
|
|
23
22
|
const assetsCache = /* @__PURE__ */ new WeakMap();
|
|
24
23
|
function computeAssetFiles(route) {
|
|
@@ -160,12 +159,7 @@ async function renderErrorPage(req, status, urlPathname, error, nonce) {
|
|
|
160
159
|
}
|
|
161
160
|
async function renderPage(req, { nonce } = {}) {
|
|
162
161
|
let { pathname } = new URL(req.url);
|
|
163
|
-
|
|
164
|
-
if (BASE_URL !== "/") {
|
|
165
|
-
const baseSlashed = BASE_URL.endsWith("/") ? BASE_URL : BASE_URL + "/";
|
|
166
|
-
const baseNoSlash = baseSlashed.slice(0, -1);
|
|
167
|
-
pathname = pathname === baseNoSlash ? "/" : pathname.slice(baseSlashed.length - 1);
|
|
168
|
-
}
|
|
162
|
+
pathname = stripBase(pathname);
|
|
169
163
|
const isJsonRequest = pathname.endsWith(".pageContext.json");
|
|
170
164
|
let targetPathname = pathname;
|
|
171
165
|
if (isJsonRequest) {
|
|
@@ -197,7 +191,7 @@ async function renderPage(req, { nonce } = {}) {
|
|
|
197
191
|
} catch (error) {
|
|
198
192
|
if (error instanceof AbortRedirect) {
|
|
199
193
|
let redirectUrl = error.url;
|
|
200
|
-
if (redirectUrl.startsWith("/")) redirectUrl =
|
|
194
|
+
if (redirectUrl.startsWith("/")) redirectUrl = BASE_URL + (redirectUrl === "/" ? "" : redirectUrl);
|
|
201
195
|
if (isJsonRequest) return Response.json({ _redirect: redirectUrl }, { status: 200 });
|
|
202
196
|
return new Response(null, {
|
|
203
197
|
status: error.statusCode,
|
package/dist/server.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { t as renderPage } from "./server-
|
|
1
|
+
import { t as renderPage } from "./server-CRcgzWMc.mjs";
|
|
2
2
|
export { renderPage };
|
package/dist/vite.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { BASE_URL } from "./__internal/shared.mjs";
|
|
2
|
+
import { t as renderPage } from "./server-CRcgzWMc.mjs";
|
|
2
3
|
import fs from "node:fs";
|
|
3
4
|
import path from "node:path";
|
|
4
5
|
import { Readable } from "node:stream";
|
|
@@ -322,19 +323,17 @@ function vikeLite({ pagesDir = "pages", apiPrefix = "/api", prerender = false, s
|
|
|
322
323
|
}
|
|
323
324
|
if (urlsToPrerender.size === 0) return;
|
|
324
325
|
console.log("[vike-lite] 📦 Starting Static Site Generation (SSG)…");
|
|
325
|
-
const { BASE_URL } = import.meta.env;
|
|
326
|
-
const baseNoSlash = BASE_URL.endsWith("/") ? BASE_URL.slice(0, -1) : BASE_URL;
|
|
327
326
|
let generatedCount = 0;
|
|
328
327
|
const clientDir = path.join(viteConfigRoot, outDir, "client");
|
|
329
328
|
for (const urlPath of urlsToPrerender) {
|
|
330
|
-
const htmlRes = await renderPage(new Request(`http://localhost${
|
|
329
|
+
const htmlRes = await renderPage(new Request(`http://localhost${BASE_URL}${urlPath}`));
|
|
331
330
|
if (htmlRes?.ok && htmlRes.headers.get("content-type")?.includes("text/html")) {
|
|
332
331
|
const outDirRoute = path.join(clientDir, urlPath === "/" ? "" : urlPath);
|
|
333
332
|
fs.mkdirSync(outDirRoute, { recursive: true });
|
|
334
333
|
fs.writeFileSync(path.join(outDirRoute, "index.html"), await htmlRes.text());
|
|
335
334
|
} else throw new Error(`[vike-lite] ❌ SSG HTML Error for "${urlPath}"`);
|
|
336
335
|
const jsonTarget = urlPath === "/" ? "/index" : urlPath;
|
|
337
|
-
const jsonRes = await renderPage(new Request(`http://localhost${
|
|
336
|
+
const jsonRes = await renderPage(new Request(`http://localhost${BASE_URL}${jsonTarget}.pageContext.json`));
|
|
338
337
|
if (jsonRes?.ok) {
|
|
339
338
|
const jsonOutPath = path.join(clientDir, `${jsonTarget}.pageContext.json`);
|
|
340
339
|
fs.mkdirSync(path.dirname(jsonOutPath), { recursive: true });
|
package/package.json
CHANGED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
//#region src/__internal/shared/matchRoute.ts
|
|
2
|
-
const regexCache = /* @__PURE__ */ new Map();
|
|
3
|
-
function escapeRegex(str) {
|
|
4
|
-
return str.replaceAll(/[.*+?^${}()|[\]\\]/g, String.raw`\$&`);
|
|
5
|
-
}
|
|
6
|
-
function matchRoute(urlPathname, routes) {
|
|
7
|
-
for (const route of routes) {
|
|
8
|
-
if (!route.path.includes(":")) {
|
|
9
|
-
if (route.path === urlPathname || route.path + "/" === urlPathname) return {
|
|
10
|
-
route,
|
|
11
|
-
routeParams: {}
|
|
12
|
-
};
|
|
13
|
-
continue;
|
|
14
|
-
}
|
|
15
|
-
let compiled = regexCache.get(route.path);
|
|
16
|
-
if (!compiled) {
|
|
17
|
-
const paramNames = [];
|
|
18
|
-
const regexPath = route.path.split("/").map((segment) => {
|
|
19
|
-
if (segment.startsWith(":")) {
|
|
20
|
-
paramNames.push(segment.slice(1));
|
|
21
|
-
return "([^/]+)";
|
|
22
|
-
}
|
|
23
|
-
return escapeRegex(segment);
|
|
24
|
-
}).join("/");
|
|
25
|
-
compiled = {
|
|
26
|
-
regex: new RegExp(`^${regexPath}/?$`),
|
|
27
|
-
paramNames
|
|
28
|
-
};
|
|
29
|
-
regexCache.set(route.path, compiled);
|
|
30
|
-
}
|
|
31
|
-
const match = urlPathname.match(compiled.regex);
|
|
32
|
-
if (match) {
|
|
33
|
-
const routeParams = {};
|
|
34
|
-
for (let i = 0; i < compiled.paramNames.length; i++) routeParams[compiled.paramNames[i]] = decodeURIComponent(match[i + 1]);
|
|
35
|
-
return {
|
|
36
|
-
route,
|
|
37
|
-
routeParams
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
return null;
|
|
42
|
-
}
|
|
43
|
-
//#endregion
|
|
44
|
-
export { matchRoute as t };
|