Skip to content

chore: ensure full page reloads between SvelteKit tutorial and other pages (and vice versa) #301

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 7 commits into from
Oct 9, 2024

Conversation

dummdidumm
Copy link
Member

Rather than having to ensure for every other page that loading images etc plays nice with the restrictive origin headers we set for the tutorial, we make navigations from/to the SvelteKit tutorial a full page reload. In practise you'll rarely cross this border, from my testing it's also barely noticeable. So the tiny drawback this has is worth the increased stability of the rest of the page to me.

WIP, this still needs to set the headers in dev and prod correctly - I'm not 100% sure how to do that since the URLs don't contain any info about sveltekit. The two approaches I see:

  • a) have some script that runs and infers the slugs from the file system
  • b) instead of being /tutorial/<slug of page> we make it /tutorial/<svelte or sveltekit, depending on which part you're on>/<slug of page>

Any opinions on which way to go? Also, objections to the overall approach?

Copy link

vercel bot commented Oct 9, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
omnisite ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 9, 2024 8:03pm

@Rich-Harris
Copy link
Member

I think this is probably the right thing to do, yeah.

In terms of implementation could it be as simple as this, in +layout.svelte...

function needs_isolation(url: URL) {
  return $page.data.needs_isolation.includes(url.pathname);
}

beforeNavigate(({ from, to }) => {
  if (needs_isolation(from.url) !== needs_isolation(to.url)) {
    location.reload();
  }
});

...where $page.data.needs_isolation looks like this?

['/tutorial/introducing-sveltekit', '/tutorial/pages', '/tutorial/layouts', ...]

@trueadm
Copy link
Collaborator

trueadm commented Oct 9, 2024

b) instead of being /tutorial/ we make it /tutorial/<svelte or sveltekit, depending on which part you're on>/

This sounds good to me.

@dummdidumm
Copy link
Member Author

In terms of implementation could it be as simple as this

I already implemented a client-side reload mechanism. The problem is the headers we need to set for the Vercel deployment. The tutorial is prerendered, therefore if you come from Google and directly visit the tutorial you'll get a prerendered shell, and the handle hook has no say in setting headers there, so it needs to be handled by either vercel.json or middleware.js. It would also be good to replicate this functionality very closely at dev time, which means having the same logic in the server middleware inside vite.config.js. In none of these situations do we have any other information besides the URL. That's why we need either a) or b):

  • a) would solve it by traversing contents/tutorial/<sveltekit parts> and infering the URLs from that, and then feed that to the Vercel and Vite config somehow
  • b) would solve it simpler by being able to just matching for /tutorial/sveltekit/*

@Rich-Harris
Copy link
Member

Ah, gotcha. Then yeah, I think /tutorial/svelte/... and /tutorial/kit/... is the way to go (i'd go with kit over sveltekit for consistency with the docs)

dummdidumm added a commit that referenced this pull request Oct 9, 2024
- gives us more leeway with duplicated slugs if needed later on
- makes URL a bit more organized
- will make it easier to set dedicated headers for the SvelteKit tutorial (#301)
- fixes a bug with redirects not being picked up due to prerendering not coming across old slugs
Rich-Harris pushed a commit that referenced this pull request Oct 9, 2024
* chore: separate tutorial URLs into svelte and kit

- gives us more leeway with duplicated slugs if needed later on
- makes URL a bit more organized
- will make it easier to set dedicated headers for the SvelteKit tutorial (#301)
- fixes a bug with redirects not being picked up due to prerendering not coming across old slugs

* fix logic

* fix a bunch of links or remove obsolete ones that have no replacement
@Rich-Harris
Copy link
Member

I'm still confused about the afterNavigate. Right now every onward navigation from a SvelteKit tutorial page causes a full reload, which means we have to go through the whole webcontainer startup rigamarole, and appending data-sveltekit-reload attributes to the DOM feels very hacky. Why aren't we using beforeNavigate instead? Opened #308

@dummdidumm
Copy link
Member Author

The logic worked prior to the tutorial slug change, and of course it can be vastly simplified now - I just didn't get around to that yet.

* use beforeNavigate

* Update apps/svelte.dev/src/routes/+layout.svelte

---------

Co-authored-by: Simon H <[email protected]>
@Rich-Harris Rich-Harris merged commit 1abac9d into main Oct 9, 2024
3 checks passed
@Rich-Harris Rich-Harris deleted the sk-tutorial-reload branch October 9, 2024 20:11
dummdidumm added a commit that referenced this pull request Oct 9, 2024
#301 wasn't quite finished, this does finalize the work:
- ensure cross origin headers are only set on SvelteKit tutorial pages
- remove obsolete workarounds for getting cross origin iframes/images/audio/video working
Rich-Harris pushed a commit that referenced this pull request Oct 9, 2024
* chore: cleanup headers, remove obsolete workarounds

#301 wasn't quite finished, this does finalize the work:
- ensure cross origin headers are only set on SvelteKit tutorial pages
- remove obsolete workarounds for getting cross origin iframes/images/audio/video working

* try this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants