I wanted one of my route in my app not to be server side rendered.
The way to do this will be doing export const ssr = false
in module script or setting ssr: false
in svelte.config.js
as mention in svelte docs
But even after disabling ssr in both the ways, I'm still getting errors in terminal like localStorage is not defined
which should be not there with ssr disabled.
While app still works find. But whenever i reload the page in browser this error appears on the terminal
[vite] Error when evaluating SSR module /src/routes/index.svelte:
ReferenceError: localStorage is not defined
svelte.config.js
import preprocess from 'svelte-preprocess';
import adapter from '@sveltejs/adapter-static';
/** @type {import('@sveltejs/kit').Config} */
const config = {
// Consult
// for more information about preprocessors
preprocess: preprocess(),
kit: {
ssr: false,
adapter: adapter({
fallback: '200.html'
}),
prerender: {
enabled: false
},
}
};
export default config;
index.svelte
<script context="module" lang="ts">
export const ssr = false
</script>
<script lang="ts">
import Yrtc from "./../helpers/Yrtc";
import { onMount } from "svelte";
onMount(() => {
const yrtc = new Yrtc({
roomId: 'tet-riieiiasds-di'
})
console.log({yrtc})
})
</script>
test
I wanted one of my route in my app not to be server side rendered.
The way to do this will be doing export const ssr = false
in module script or setting ssr: false
in svelte.config.js
as mention in svelte docs
But even after disabling ssr in both the ways, I'm still getting errors in terminal like localStorage is not defined
which should be not there with ssr disabled.
While app still works find. But whenever i reload the page in browser this error appears on the terminal
[vite] Error when evaluating SSR module /src/routes/index.svelte:
ReferenceError: localStorage is not defined
svelte.config.js
import preprocess from 'svelte-preprocess';
import adapter from '@sveltejs/adapter-static';
/** @type {import('@sveltejs/kit').Config} */
const config = {
// Consult https://github./sveltejs/svelte-preprocess
// for more information about preprocessors
preprocess: preprocess(),
kit: {
ssr: false,
adapter: adapter({
fallback: '200.html'
}),
prerender: {
enabled: false
},
}
};
export default config;
index.svelte
<script context="module" lang="ts">
export const ssr = false
</script>
<script lang="ts">
import Yrtc from "./../helpers/Yrtc";
import { onMount } from "svelte";
onMount(() => {
const yrtc = new Yrtc({
roomId: 'tet-riieiiasds-di'
})
console.log({yrtc})
})
</script>
test
Share
Improve this question
asked Oct 2, 2021 at 5:31
GAGANDEEP SINGHGAGANDEEP SINGH
1,0562 gold badges12 silver badges15 bronze badges
1
- 2 This is currently a limitation, see github./sveltejs/kit/issues/1650 for more info. github./sveltejs/kit/pull/2529 might fix this if you are going SPA-only for the whole app and don't need any SSR capabilities. – dummdidumm Commented Oct 3, 2021 at 9:04
2 Answers
Reset to default 0At least nowadays (tested with SvelteKit 1.20.4), this works as advertised. Note that a few things in the question's example code were removed or are not necessary to solve the problem:
const config = {
// ...
kit: {
ssr: false,
prerender: {
enabled: false
},
// ...
}
};
These two options individually lead to the errors "Unexpected option config.kit.ssr" and "Unexpected option config.kit.prerender.enabled" respectively, and will abort the build.
<script context="module">
export const ssr = false
</script>
Independently of whether it's in the module
script, this leads to the following warning: "export const ssr
will be ignored — move it to +page(.server).js/ts instead. See https://kit.svelte.dev/docs/page-options for more information."
The reason for requiring this is that reading the ssr
property exported from some +page.svelte
requires actually loading the ponent, including imports, at which point it is too late for preventing the issue.
If the export is moved to either of these files (+page(.server).js/ts
), it will work. Steps to reproduce:
- create a new project (use demo app template):
npm create svelte@latest no-ssr-test && cd no-ssr-test && npm i
- in
src/routes/+page.svelte
, insertconsole.log(window)
- run using
npm run dev
to verify you getReferenceError: window is not defined
- in
src/routes/+page.js
(or similar), insertexport const ssr = false
- run using
npm run dev
to verify the site works again
Variables like localStorage
, document
and self
are not defined on the server. Use those variables inside onMount
function which is only run in the browser when the ponent has been rendered:
<script>
import { onMount } from 'svelte';
onMount(() => {
document.createElement(...);
// ...
});
</script>
Source: https://stackoverflow./a/56184892/9157799
If you're importing an external module that depends on those variables, use the import()
function to load the module at runtime:
<script>
import { onMount } from 'svelte'
onMount(async () => {
const Plotly = await import('plotly.js-dist-min')
// ...
})
</script>
Source: https://stackoverflow./a/65452698/9157799