Skip to content

Commit

Permalink
fix(qwik-city): redirect route loads when sane (#6740)
Browse files Browse the repository at this point in the history
When using capacitor, the "browser" is on capacitor://localhost or http://localhost, and
when route loading is proxied, the return URL origin is not the same as the current
origin. This prompted qwik-city to reload the page.

Now, this is only done when the request was actually redirected.
  • Loading branch information
wmertens authored Jul 29, 2024
1 parent d0e6279 commit f5aabb9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/nasty-files-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@builder.io/qwik-city': patch
---

qwik-city is now more careful about redirects after requesting routeLoader data
17 changes: 11 additions & 6 deletions packages/qwik-city/src/runtime/src/use-endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const loadClientData = async (
const pagePathname = url.pathname;
const pageSearch = url.search;
const clientDataPath = getClientDataPath(pagePathname, pageSearch, opts?.action);
let qData = undefined;
let qData: Promise<ClientPageData | undefined> | undefined;
if (!opts?.action) {
qData = CLIENT_DATA_CACHE.get(clientDataPath);
}
Expand All @@ -33,24 +33,29 @@ export const loadClientData = async (
opts.action.data = undefined;
}
qData = fetch(clientDataPath, fetchOptions).then((rsp) => {
const redirectedURL = new URL(rsp.url);
const isQData = redirectedURL.pathname.endsWith('/q-data.json');
if (redirectedURL.origin !== location.origin || !isQData) {
location.href = redirectedURL.href;
return;
if (rsp.redirected) {
const redirectedURL = new URL(rsp.url);
const isQData = redirectedURL.pathname.endsWith('/q-data.json');
if (!isQData || redirectedURL.origin !== location.origin) {
// Captive portal etc. We can't talk to the server, so redirect as asked
location.href = redirectedURL.href;
return;
}
}
if ((rsp.headers.get('content-type') || '').includes('json')) {
// we are safe we are reading a q-data.json
return rsp.text().then((text) => {
const clientData = _deserializeData(text, element) as ClientPageData | null;
if (!clientData) {
// Something went wrong, show to the user
location.href = url.href;
return;
}
if (opts?.clearCache) {
CLIENT_DATA_CACHE.delete(clientDataPath);
}
if (clientData.redirect) {
// server function asked for redirect
location.href = clientData.redirect;
} else if (opts?.action) {
const { action } = opts;
Expand Down

0 comments on commit f5aabb9

Please sign in to comment.