最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Error when evaluating SSR module even when SSR is disabled - svelte-kit - Stack Overflow

programmeradmin5浏览0评论

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
Add a ment  | 

2 Answers 2

Reset to default 0

At 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, insert console.log(window)
  • run using npm run dev to verify you get ReferenceError: window is not defined
  • in src/routes/+page.js (or similar), insert export 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

发布评论

评论列表(0)

  1. 暂无评论