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

[feat] pass ComponentLoader to handle #2663

Closed
wants to merge 1 commit into from
Closed

Conversation

benmccann
Copy link
Member

@benmccann benmccann commented Oct 22, 2021

This is needed if you want to load any .svelte file from handle

One potential usecase would be allowing you to use any other router with SvelteKit allowing you to leverage SvelteKit adapters, HMR, etc. without using our router. I'm sure there'd be other use cases as well.

I tried using this with Routify and it basically worked except that Routify doesn't seem to support SSR.

Even if we don't expose this yet, separating out ComponentLoader from the routing options might be a nice change.

I have purposefully not documented this as I consider it an experimental API still. We can add it to the docs after we flesh it out.

@changeset-bot
Copy link

changeset-bot bot commented Oct 22, 2021

🦋 Changeset detected

Latest commit: e5723d9

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@benmccann benmccann changed the title [chore] separate Vite loader from Kit options [feat] pass ViteLoader to resolve Oct 22, 2021
@benmccann benmccann changed the title [feat] pass ViteLoader to resolve [feat] pass ComponentLoader to resolve Oct 22, 2021
@benmccann benmccann marked this pull request as draft October 22, 2021 18:51
@benmccann benmccann marked this pull request as ready for review October 22, 2021 19:22
@benmccann benmccann force-pushed the vite-loader branch 6 times, most recently from f2ef5d7 to e1eea0b Compare October 22, 2021 21:27
@benmccann benmccann changed the title [feat] pass ComponentLoader to resolve [feat] pass ComponentLoader to handle Oct 22, 2021
@benmccann benmccann force-pushed the vite-loader branch 7 times, most recently from aee990b to 6424a96 Compare October 22, 2021 22:36
@bluwy bluwy self-requested a review October 23, 2021 04:08
@benmccann benmccann marked this pull request as draft November 4, 2021 05:06
@jakobrosenberg
Copy link

Been having a blast experimenting with this today.

It seems there are two approaches for adding third party routers (sorry if I'm stating the obvious):

Option A - loadComponent

hooks.js

import { Router, createRouter } from '@roxi/routify'
import routes from '../.routify/routes.default.js'

/** @type {import('@sveltejs/kit').Handle} */
export async function handle({ request, loader }) {

    // create a router instance
    const router = createRouter({ routes })

    // let router run async functions, eg. fetch dynamic imports, run preloads, guards etc.
    await router.url.replace(request.path) 

    // create SSR node    
    const ssrNode = await loader.loadComponent({ id: 'src/routes/root.svelte' });
    
    // inject the router into  `src/routes/root.svelte`
    const rendered = ssrNode.module.default.render({ router });    
...

src/routes/root.svelte

<script>
    import { Router } from '@roxi/routify'
    export let router
</script>

<Router {router} />

Option B - plain SSR

hooks.js

import { Router, createRouter } from '@roxi/routify'
import routes from '../.routify/routes.default.js'

/** @type {import('@sveltejs/kit').Handle} */
export async function handle({ request, loader }) {

    const router = createRouter({ routes })
    await router.url.replace(request.path)
    const rendered = Router.render({router})

Option B doesn't require a root.svelte file, but I'm not sure about pros and cons of using loadComponent

@benmccann
Copy link
Member Author

Option B will not return the list of JS and CSS files needed to populate the page's head the way that loadComponent will

@Rich-Harris
Copy link
Member

I'm afraid I think this is a bit of a non-starter, aside from whether it's a desirable API — it makes server-side code-splitting completely impossible.

Supporting other routers is a non-goal as far as I'm concerned — the router is a large part of what SvelteKit is. So my inclination is to close this, unless there's another use case that we want to try and enable?

@benmccann
Copy link
Member Author

I do think there are cases where you might want to use a different router as @pngwn frequently points out. And I don't think that SSR code splitting would necessarily be a blocker since not all adapters support it and it's an optional feature on those that do. That being said, this PR is out-of-date and I'm not tremendously motivated to update it since the Routify folks never did much with it

@benmccann benmccann closed this Jan 11, 2022
@Conduitry Conduitry deleted the vite-loader branch January 11, 2022 01:32
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