Skip to content

use beforeNavigate #308

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

Merged
merged 2 commits into from
Oct 9, 2024
Merged
Changes from 1 commit
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
28 changes: 9 additions & 19 deletions apps/svelte.dev/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,19 @@
import { Nav } from '@sveltejs/site-kit/nav';
import { Search, SearchBox } from '@sveltejs/site-kit/search';
import { injectSpeedInsights } from '@vercel/speed-insights/sveltekit';
import { afterNavigate } from '$app/navigation';
import { needs_webcontainers } from './tutorial/[...slug]/shared';
import type { Exercise } from '$lib/tutorial';
import { beforeNavigate } from '$app/navigation';

injectSpeedInsights();

let exercise: Exercise | undefined = undefined;

afterNavigate(() => {
// Make all navigations between SvelteKit-tutorial and non-SvelteKit-tutorial pages (and vice versa)
// a full page navigation to ensure webcontainers get the correct origin restriction headers while
// ensuring those headers don't interfere with the rest of the page. These headers would have bad
// consequences on how we have to handle integration of images etc from other domains for example.
if ($page.data?.exercise && needs_webcontainers($page.data.exercise)) {
document.body.setAttribute('data-sveltekit-reload', 'true');
if (!!exercise && !needs_webcontainers(exercise)) {
location.reload();
}
} else {
document.body.removeAttribute('data-sveltekit-reload');
}
beforeNavigate(({ from, to, cancel }) => {
if (!from || !to) return;

exercise = $page.data?.exercise;
if (
from.url.pathname.startsWith('/tutorial/kit/') !== to.url.pathname.startsWith('/tutorial/kit')
) {
cancel();
location.href = to.url.href;
}
});

let { data, children: layout_children } = $props();
Expand Down
Loading