Skip to content

Commit 9a44958

Browse files
committed
fix: SPA routing is broken unless origin matches value in in vite.config
1 parent 6cc1d22 commit 9a44958

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

packages/qwik-city/src/runtime/src/qwik-city-component.tsx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,13 +139,19 @@ export const QwikCityProvider = component$<QwikCityProps>((props) => {
139139
);
140140
const navResolver: { r?: () => void } = {};
141141
const loaderState = _weakSerialize(useStore(env.response.loaders, { deep: false }));
142+
143+
// The initial state of routeInternal uses the URL provided by the server environment.
144+
// It may not be accurate to the actual URL the browser is accessing the site from.
145+
// It is useful for the purposes of SSR and SSG, but may be overridden browser-side
146+
// if needed for SPA routing.
142147
const routeInternal = useSignal<RouteStateInternal>({
143148
type: 'initial',
144149
dest: url,
145150
forceReload: false,
146151
replaceState: false,
147152
scroll: true,
148153
});
154+
149155
const documentHead = useStore<Editable<ResolvedDocumentHead>>(createDocumentHead);
150156
const content = useStore<Editable<ContentState>>({
151157
headings: undefined,
@@ -218,6 +224,15 @@ export const QwikCityProvider = component$<QwikCityProps>((props) => {
218224
} = typeof opt === 'object' ? opt : { forceReload: opt };
219225
internalState.navCount++;
220226

227+
// If this is the first SPA navigation, we rewrite routeInternal's URL
228+
// as the browser location URL to prevent an erroneous origin mismatch.
229+
// The initial value of routeInternal is derived from the server env,
230+
// which in the case of SSG may not match the actual origin the site
231+
// is deployed on.
232+
if (isBrowser && routeInternal.value.type === 'initial') {
233+
routeInternal.value.dest = new URL(window.location.href);
234+
}
235+
221236
const lastDest = routeInternal.value.dest;
222237
const dest =
223238
path === undefined

0 commit comments

Comments
 (0)