question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Woes with SSR and Promises

See original GitHub issue

I’m having trouble setting up SSR in a Svelte project that utilizes dynamic imports. I’m bundling the app with Rollup, producing a single JS file with dynamic imports inlined.

Best described with code:

<script>
	const params = {};
	const go = () => import('./views/PageHome.svelte');
</script>

{#await go()}
	Loading...
{:then comp}
	<svelte:component this={comp.default} {params} />
{/await}

…rendering this on the server will yield

{ html: '\n\t\tLoading...\n\t', css: { code: '', map: null }, head: '' }

Similarly, getting rid of the {#await ...} block and just setting the route directly like so…

<script>
	const params = {};
	let Route;

	import('./views/PageHome.svelte').then(comp => Route = comp.default);
</script>

<svelte:component this={Route} {params} />

…will yield:

{ html: '', css: { code: '', map: null }, head: '' }

The above code is compiled into this:

const App = create_ssr_component(($$result, $$props, $$bindings, $$slots) => {
	const params = {};
	let Route;

	Promise.resolve().then(function () { return PageHome$1; }).then(comp => Route = comp.default);

	return `${validate_component(((Route) || missing_component), 'svelte:component').$$render($$result, { params: params }, {}, {})}`;
});

The issue of course is the promise here.

It’s pretty common to have an app that uses route based code-splitting. How does one, with Svelte, handle server-side rendering in that case? Extract routing outside of the Svelte app, and just render the specific Svelte page components directly?

Are there any good solutions to combining SSR and code-splitting and if so, should they be documented?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
CaptainNcommented, Nov 5, 2019

I do wonder if there is a way Svelte could be adjusted to render resolved promises immediately instead of waiting a minimum of one tick. It could be a nice cheap performance win.

0reactions
CaptainNcommented, May 11, 2020

Even with an already-resolved promise, there is no way to get its value synchronously.

Not through the promise, but there are ways to work around that. I set up svelte-loadable that way. It registers the “loaders” so that there is a state handler that works outside of the promises. A pattern like that might not work in every case, I only mention it as an example of how to get around that problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How To Avoid SSR Load Issues in Node.js - NodeSource
SSR performance issues typically fall into two categories: data fetching and rendering. Either the data fetching takes too long, ...
Read more >
A pain in the react: Challenges behind SSR | Nckweb
In this post I'll try to show what, in my opinion, are the current pain points on the common ways to do server...
Read more >
Server-side rendering - Apollo GraphQL Docs
One solution to this problem is to use an Apollo Link to fetch data using a local graphql schema instead of making a...
Read more >
Why Server Side Rendering In React Is So Hard - Farmdev
This is a problem for SSR because a component's data is already loaded when client side rendering begins. If a naive loader like...
Read more >
Challenges in server side rendering React apps (SSR)
mkdir react-ssr cd react-ssr yarn init (Accept defaults) yarn add react@next react-dom@next · import React from 'react'; export default function ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found