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.

Explicit SSR Imports

See original GitHub issue

Is your feature request related to a problem? Please describe. In short, the issue here is caused by how SSR is generated and bundled. When import client side libraries such as firebase or js-cookie I have found that the site does not run because the client libraries are still being imported into the server runtime resulting in SSR errors.

Describe the solution you’d like Ideally there would be a vite extension that can interpret imports accordingly such that the imports can be top level like they are in sapper. From my experience I did not have this issue when using Sapper, only with sveltekit.

Describe alternatives you’ve considered One possible alternative would be to have a config file where you can explicitly declare which imports are server and which imports are client side. Not really sure how the sapper stack handled it but I could not reproduce a similar issue with sapper.

How important is this feature to you? This feature is not direly important, but it is a quality of life feature and most developers might be confused by the inability for them to easily import modules at the top level of their script blocks without causing various warnings or errors.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:10
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

4reactions
DrewRidleycommented, Apr 9, 2021

I am not necessarily referencing a true SPA scenario. I was thinking something more along the lines of sapper. I still want SSR to be enabled, but I would like certain imports that are client-only to still feel natural. I can look into the how the vite plugin works and its possible this can be adapted to be added. I understand that vite is very different from rollup so I dont know how feasible it is.

2reactions
jthegeduscommented, Jun 18, 2021

@cupcakearmy It depends on the Firebase modules you are using. firebase/firestore, which most people are using, doesn’t have a dependency on IDBIndex so aren’t facing this issue.

~This is feedback the Firebase team would like to receive. I have observed the IDBIndex error you report when trying to use the firebase/performance module.~

~The vite.optimizeDeps.include[] config doesn’t resolve the issue.~

Also of note, the latest npm v9 SDK has changed from firebase@exp to firebase@beta


Update: it works when you dynamically import the library.

// $lib/firebaseClient.js
import { initializeApp } from "firebase/app";
import { getPerformance } from "firebase/performance";

const app = initializeApp({
	...
});

function performance() {
	return getPerformance(app);
}

export { performance };
<script>
	import { browser } from "$app/env";
	import { onMount } from "svelte";

	// this does not work
	// import { performance } from "$lib/firebaseClient"

	let perf;

	onMount(async () => {
		if (browser) {
			// this works
			const firebaseClient = await import("$lib/firebaseClient");
			perf = firebaseClient.performance(); // this initialises the perf-mon module.
		}
	})	
</script>

The key thing here is that firebaseClient and thus, firebase/performance is not imported and evaluated until the required window.db is available.

An alternative is to push the await import() into firebaseClient
// $lib/firebaseClient.js
import { initializeApp } from "firebase/app";

const app = initializeApp({
	...
});

async function performance() {
	const perf = await import("firebase/performance");
	return perf.getPerformance(app);
}

export { performance };
<script>
	import { browser } from "$app/env";
	import { onMount } from "svelte";
	import { performance } from "$lib/firebaseClient"

	let perf;

	onMount(async () => {
		if (browser) {
			perf = await performance();
		}
	})	
</script>
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use client-side only packages with SSR in Gatsby and ...
I have a question btw: Next docs explicitly say that it is forbidden to use next/dynamic imports inside of react components like it...
Read more >
reactjs - Conditionally import module using next js dynamic ...
I've a component which I am dynamically importing using dynamic from nextjs. And that too I want ...
Read more >
Advanced Features: Dynamic Import - Next.js
Dynamically import JavaScript modules and React Components and split your code into manageable chunks.
Read more >
Server Side Rendering – React Aria
import {SSRProvider} from 'react-aria'; <SSRProvider> <App /> </SSRProvider> ... an explicit locale prop, rather than relying on automatic locale selection.
Read more >
Static Asset Handling - Vite
Explicit URL Imports # · workletURL from 'extra-scalloped-border/worklet.js?url' ; Importing Asset as String # · shaderString from './shader.glsl?raw' ; Importing ...
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