@alignable/bifrost 1.0.24 → 1.0.25
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.
|
@@ -5,20 +5,10 @@ import {
|
|
|
5
5
|
// renderer/wrapped/onBeforeRender.client.ts
|
|
6
6
|
import { redirect } from "vike/abort";
|
|
7
7
|
|
|
8
|
-
// lib/
|
|
9
|
-
async function
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
await new Promise(() => {
|
|
13
|
-
});
|
|
14
|
-
return;
|
|
15
|
-
}
|
|
16
|
-
const parsedUrl = new URL(url);
|
|
17
|
-
if (window.location.origin === parsedUrl.origin) {
|
|
18
|
-
window.location.href = parsedUrl.pathname + parsedUrl.search + parsedUrl.hash;
|
|
19
|
-
} else {
|
|
20
|
-
window.location.href = url;
|
|
21
|
-
}
|
|
8
|
+
// lib/hardNavigate.ts
|
|
9
|
+
async function hardNavigate(url) {
|
|
10
|
+
history.pushState(null, "", url);
|
|
11
|
+
window.Turbolinks.controller.viewInvalidated();
|
|
22
12
|
await new Promise(() => {
|
|
23
13
|
});
|
|
24
14
|
}
|
|
@@ -31,7 +21,9 @@ async function wrappedOnBeforeRender(pageContext) {
|
|
|
31
21
|
}).catch(() => {
|
|
32
22
|
});
|
|
33
23
|
if (!resp) {
|
|
34
|
-
|
|
24
|
+
window.location.href = pageContext.urlParsed.href;
|
|
25
|
+
await new Promise(() => {
|
|
26
|
+
});
|
|
35
27
|
return;
|
|
36
28
|
}
|
|
37
29
|
if (resp.redirected) {
|
|
@@ -39,20 +31,18 @@ async function wrappedOnBeforeRender(pageContext) {
|
|
|
39
31
|
if (window.location.origin === parsedUrl.origin) {
|
|
40
32
|
throw redirect(parsedUrl.pathname + parsedUrl.search + parsedUrl.hash);
|
|
41
33
|
} else {
|
|
42
|
-
|
|
43
|
-
return;
|
|
34
|
+
throw redirect(resp.url);
|
|
44
35
|
}
|
|
45
36
|
}
|
|
46
37
|
if (!resp.ok) {
|
|
47
|
-
await
|
|
48
|
-
return;
|
|
38
|
+
await hardNavigate(resp.url);
|
|
49
39
|
}
|
|
50
40
|
const html = await resp.text();
|
|
51
41
|
const layoutInfo = pageContext.config.getLayout(
|
|
52
42
|
Object.fromEntries(resp.headers.entries())
|
|
53
43
|
);
|
|
54
44
|
if (!layoutInfo) {
|
|
55
|
-
await
|
|
45
|
+
await hardNavigate(resp.url);
|
|
56
46
|
return;
|
|
57
47
|
}
|
|
58
48
|
const parsed = document.createElement("html");
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../renderer/wrapped/onBeforeRender.client.ts","../../../lib/
|
|
1
|
+
{"version":3,"sources":["../../../renderer/wrapped/onBeforeRender.client.ts","../../../lib/hardNavigate.ts"],"sourcesContent":["import \"../../lib/type\";\nimport type { PageContextClient } from \"vike/types\";\nimport { redirect } from \"vike/abort\";\nimport { getElementAttributes } from \"../../lib/elementUtils\";\nimport { hardNavigate } from \"../../lib/hardNavigate\";\n\n// onBeforeRender runs before changing the browser location, so `throw redirect` works\n// we wait for onBeforeRenderClient to call mergeHead, which runs after browser location change\n// Possibly could move this back into onBeforeRenderClient: https://github.com/vikejs/vike/pull/2820\nexport default async function wrappedOnBeforeRender(\n pageContext: PageContextClient\n) {\n if (\n pageContext.isClientSide &&\n !pageContext?._snapshot &&\n !pageContext.isHydration\n ) {\n /*\n Mermaid diagram of client side navigation logic:\n\n Vike Router --> Proxy Mode\n Proxy Mode -->|wrapped| Request Legacy Backend\n Request Legacy Backend -->|redirect| Vike Router\n Request Legacy Backend -->|html| Render Wrapped Page\n Proxy Mode -->|false| Render Vike Page\n Proxy Mode -->|passthru| Browser Navigation\n\n ┌─────────────┐ ┌────────────┐ ┌────────────────────────┐ ┌─────────────────────┐\n │ │ │ │ │ │ │ │\n │ Vike Router ├────►│ Proxy Mode ├─wrapped►│ Request Legacy Backend ├html─►│ Render Wrapped Page │\n │ │ │ │ │ │ │ │\n └─────────────┘ └──────┬─────┘ └────────────┬───────────┘ └─────────────────────┘\n ▲ │ redirect \n └───────────────────┼────────────────────────────┘ \n false \n │ ┌────────────────────────┐ \n │ │ │ \n ├──────────────►│ Render Vike Page │ \n passthru │ │ \n │ └────────────────────────┘ \n │ \n │ ┌────────────────────────┐ \n │ │ │ \n └──────────────►│ Browser Navigation │ \n │ │ \n └────────────────────────┘ \n\n The Vike router must run on every redirect, because the legacy backend could redirect to a Vike page.\n The browser follows redirects automatically, which hits the vike server, which will passthru if needed\n It would be more performant to run the Vike router on the client, but the browser does not expose redirect info.\n Optimization: use serviceworker to intercept redirects.\n */\n const resp = await fetch(pageContext.urlParsed.href, {\n headers: { ...pageContext.config.proxyHeaders, accept: \"text/html\" },\n }).catch(() => {});\n\n if (!resp) {\n // hard reload. can happen on cors errors when redirected to external page\n window.location.href = pageContext.urlParsed.href;\n // stop vike rendering to let navigation happen\n await new Promise(() => {});\n return;\n }\n\n if (resp.redirected) {\n const parsedUrl = new URL(resp.url);\n // Need to redirect to run vike router (in case redirect is not wrapped page)\n // Downside is we will make another network request\n // TODO: Can we prevent the double request? Move to server side and throw redirect on 3xx?\n if (window.location.origin === parsedUrl.origin) {\n // redirect needs to start with \"/\" or vike will do hard reload\n throw redirect(parsedUrl.pathname + parsedUrl.search + parsedUrl.hash);\n } else {\n // external redirect\n throw redirect(resp.url);\n }\n }\n if (!resp.ok) {\n await hardNavigate(resp.url);\n }\n const html = await resp.text();\n const layoutInfo = pageContext.config.getLayout!(\n Object.fromEntries(resp.headers.entries())\n );\n if (!layoutInfo) {\n // Fallback to full reload if layout not found\n await hardNavigate(resp.url);\n return;\n }\n\n const parsed = document.createElement(\"html\");\n parsed.innerHTML = html;\n const bodyEl = parsed.querySelector(\"body\")!;\n const headEl = parsed.querySelector(\"head\")!;\n pageContext.proxyLayoutInfo = layoutInfo;\n pageContext._turbolinksProxy = {\n body: bodyEl,\n head: headEl,\n bodyAttrs: getElementAttributes(bodyEl),\n };\n }\n}\n","/**\n * Hard navigate to a URL using `history.pushState` and `window.location.reload` instead of\n * `window.location.href`. This prevents the mobile apps from opening the URL in a browser tab.\n */\nexport async function hardNavigate(url: string): Promise<never> {\n history.pushState(null, \"\", url);\n window.Turbolinks.controller.viewInvalidated();\n // stop vike rendering to let navigation happen\n await new Promise(() => {});\n}\n"],"mappings":";;;;;AAEA,SAAS,gBAAgB;;;ACEzB,eAAsB,aAAa,KAA6B;AAC9D,UAAQ,UAAU,MAAM,IAAI,GAAG;AAC/B,SAAO,WAAW,WAAW,gBAAgB;AAE7C,QAAM,IAAI,QAAQ,MAAM;AAAA,EAAC,CAAC;AAC5B;;;ADAA,eAAO,sBACL,aACA;AACA,MACE,YAAY,gBACZ,CAAC,aAAa,aACd,CAAC,YAAY,aACb;AAoCA,UAAM,OAAO,MAAM,MAAM,YAAY,UAAU,MAAM;AAAA,MACnD,SAAS,EAAE,GAAG,YAAY,OAAO,cAAc,QAAQ,YAAY;AAAA,IACrE,CAAC,EAAE,MAAM,MAAM;AAAA,IAAC,CAAC;AAEjB,QAAI,CAAC,MAAM;AAET,aAAO,SAAS,OAAO,YAAY,UAAU;AAE7C,YAAM,IAAI,QAAQ,MAAM;AAAA,MAAC,CAAC;AAC1B;AAAA,IACF;AAEA,QAAI,KAAK,YAAY;AACnB,YAAM,YAAY,IAAI,IAAI,KAAK,GAAG;AAIlC,UAAI,OAAO,SAAS,WAAW,UAAU,QAAQ;AAE/C,cAAM,SAAS,UAAU,WAAW,UAAU,SAAS,UAAU,IAAI;AAAA,MACvE,OAAO;AAEL,cAAM,SAAS,KAAK,GAAG;AAAA,MACzB;AAAA,IACF;AACA,QAAI,CAAC,KAAK,IAAI;AACZ,YAAM,aAAa,KAAK,GAAG;AAAA,IAC7B;AACA,UAAM,OAAO,MAAM,KAAK,KAAK;AAC7B,UAAM,aAAa,YAAY,OAAO;AAAA,MACpC,OAAO,YAAY,KAAK,QAAQ,QAAQ,CAAC;AAAA,IAC3C;AACA,QAAI,CAAC,YAAY;AAEf,YAAM,aAAa,KAAK,GAAG;AAC3B;AAAA,IACF;AAEA,UAAM,SAAS,SAAS,cAAc,MAAM;AAC5C,WAAO,YAAY;AACnB,UAAM,SAAS,OAAO,cAAc,MAAM;AAC1C,UAAM,SAAS,OAAO,cAAc,MAAM;AAC1C,gBAAY,kBAAkB;AAC9B,gBAAY,mBAAmB;AAAA,MAC7B,MAAM;AAAA,MACN,MAAM;AAAA,MACN,WAAW,qBAAqB,MAAM;AAAA,IACxC;AAAA,EACF;AACF;","names":[]}
|