Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(qwik-city): redirect route loads when sane #6740

Merged
merged 1 commit into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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