Skip to content
Advertisement

How to generate a static website out of Sveltekit official RealWorld example for hosting on google firebase?

https://realworld.svelte.dev/ is the official example of SvelteKit.
https://github.com/sveltejs/realworld is the codebase for this official example.

I am trying to generate a static site so I can host this on google firebase. I updated config as 

content of file svelte.config.js is

import adapter from '@sveltejs/adapter-static';

export default {
    kit: {
        adapter: adapter({
            pages: 'build',
            assets: 'build',
            fallback: null,
            precompress: false,
            strict: true
        })
    }
};

Content of file src/routes/+layout.server.js is

export const prerender = true;

/** @type {import('./$types').LayoutServerLoad} */
export function load({ locals }) {
    return {
        user: locals.user && {
            username: locals.user.username,
            email: locals.user.email,
            image: locals.user.image,
            bio: locals.user.bio
        }
    };
}

When I am trying to build using command npm run build I am getting error

alok@alok-HP-Laptop-14s-cf3xxx:~/exp/test-svelte/realworld$ npm run build

> realworld.svelte.dev@1.0.1-next.0 build
> vite build


vite v4.0.3 building for production...
✓ 82 modules transformed.
4:48:04 pm [vite-plugin-svelte] dom compile done.
package                 files    time      avg
realworld.svelte.dev       25   0.61s   24.3ms

vite v4.0.3 building SSR bundle for production...
✓ 91 modules transformed.
4:48:04 pm [vite-plugin-svelte] ssr compile done.
package                 files    time     avg
realworld.svelte.dev       25   0.14s   5.4ms
.svelte-kit/output/server/vite-manifest.json                                        7.25 kB
.svelte-kit/output/server/chunks/constants.js                                       0.15 kB
.svelte-kit/output/server/chunks/hooks.server.js                                    0.18 kB
.svelte-kit/output/server/entries/pages/profile/_page.svelte.js                     0.20 kB
.svelte-kit/output/server/entries/pages/profile/_page.js                            0.22 kB
.svelte-kit/output/server/entries/pages/profile/@_user_/favorites/_page.server.js   0.22 kB
.svelte-kit/output/server/entries/pages/profile/@_user_/_layout.server.js           0.23 kB
.svelte-kit/output/server/entries/pages/_layout.server.js                           0.26 kB
.svelte-kit/output/server/chunks/ListErrors.js                                      0.51 kB
.svelte-kit/output/server/entries/pages/profile/@_user_/_page.svelte.js             0.52 kB
.svelte-kit/output/server/entries/pages/profile/@_user_/favorites/_page.svelte.js   0.52 kB
.svelte-kit/output/server/chunks/get_articles.js                                    0.55 kB
.svelte-kit/output/server/chunks/stores.js                                          0.56 kB
.svelte-kit/output/server/entries/pages/editor/_page.svelte.js                      0.63 kB
.svelte-kit/output/server/entries/pages/editor/_slug_/_page.svelte.js               0.68 kB
.svelte-kit/output/server/entries/pages/login/_page.server.js                       0.68 kB
.svelte-kit/output/server/entries/pages/register/_page.server.js                    0.75 kB
.svelte-kit/output/server/entries/pages/_page.server.js                             0.75 kB
.svelte-kit/output/server/entries/pages/editor/_page.server.js                      0.80 kB
.svelte-kit/output/server/entries/pages/profile/@_user_/_page.server.js             0.81 kB
.svelte-kit/output/server/entries/pages/_error.svelte.js                            0.92 kB
.svelte-kit/output/server/entries/pages/editor/_slug_/_page.server.js               0.94 kB
.svelte-kit/output/server/entries/pages/settings/_page.server.js                    0.96 kB
.svelte-kit/output/server/chunks/api.js                                             0.96 kB
.svelte-kit/output/server/chunks/index2.js                                          1.41 kB
.svelte-kit/output/server/entries/pages/login/_page.svelte.js                       1.45 kB
.svelte-kit/output/server/entries/pages/register/_page.svelte.js                    1.60 kB
.svelte-kit/output/server/entries/pages/article/_slug_/_page.server.js              1.75 kB
.svelte-kit/output/server/chunks/Editor.js                                          2.25 kB
.svelte-kit/output/server/entries/pages/settings/_page.svelte.js                    2.38 kB
.svelte-kit/output/server/chunks/index3.js                                          2.59 kB
.svelte-kit/output/server/entries/pages/profile/@_user_/_layout.svelte.js           2.76 kB
.svelte-kit/output/server/entries/pages/_layout.svelte.js                           2.89 kB
.svelte-kit/output/server/entries/pages/_page.svelte.js                             3.46 kB
.svelte-kit/output/server/chunks/index.js                                           3.62 kB
.svelte-kit/output/server/entries/pages/article/_slug_/_page.svelte.js              5.87 kB
.svelte-kit/output/server/index.js                                                 78.86 kB
Error: Cannot access url.searchParams on a page with prerendering enabled
    at URL.get [as searchParams] (file:///home/alok/exp/test-svelte/realworld/.svelte-kit/output/server/index.js:157:15)
    at load (file:///home/alok/exp/test-svelte/realworld/.svelte-kit/output/server/entries/pages/_page.server.js:4:19)
    at load_server_data (file:///home/alok/exp/test-svelte/realworld/.svelte-kit/output/server/index.js:517:42)
    at file:///home/alok/exp/test-svelte/realworld/.svelte-kit/output/server/index.js:1506:24
Error: Cannot prerender pages with actions
    at render_page (file:///home/alok/exp/test-svelte/realworld/.svelte-kit/output/server/index.js:1454:15)
    at async resolve (file:///home/alok/exp/test-svelte/realworld/.svelte-kit/output/server/index.js:2303:22)
    at async respond (file:///home/alok/exp/test-svelte/realworld/.svelte-kit/output/server/index.js:2205:22)
    at async visit (file:///home/alok/exp/test-svelte/realworld/node_modules/@sveltejs/kit/src/core/prerender/prerender.js:190:20)
Error: Cannot prerender pages with actions
    at render_page (file:///home/alok/exp/test-svelte/realworld/.svelte-kit/output/server/index.js:1454:15)
    at async resolve (file:///home/alok/exp/test-svelte/realworld/.svelte-kit/output/server/index.js:2303:22)
    at async respond (file:///home/alok/exp/test-svelte/realworld/.svelte-kit/output/server/index.js:2205:22)
    at async visit (file:///home/alok/exp/test-svelte/realworld/node_modules/@sveltejs/kit/src/core/prerender/prerender.js:190:20)
  307 /profile -> /login
Error: Cannot prerender pages with actions
    at render_page (file:///home/alok/exp/test-svelte/realworld/.svelte-kit/output/server/index.js:1454:15)
    at async resolve (file:///home/alok/exp/test-svelte/realworld/.svelte-kit/output/server/index.js:2303:22)
    at async respond (file:///home/alok/exp/test-svelte/realworld/.svelte-kit/output/server/index.js:2205:22)
    at async visit (file:///home/alok/exp/test-svelte/realworld/node_modules/@sveltejs/kit/src/core/prerender/prerender.js:190:20)
Error: Cannot prerender pages with actions
    at render_page (file:///home/alok/exp/test-svelte/realworld/.svelte-kit/output/server/index.js:1454:15)
    at async resolve (file:///home/alok/exp/test-svelte/realworld/.svelte-kit/output/server/index.js:2303:22)
    at async respond (file:///home/alok/exp/test-svelte/realworld/.svelte-kit/output/server/index.js:2205:22)
    at async visit (file:///home/alok/exp/test-svelte/realworld/node_modules/@sveltejs/kit/src/core/prerender/prerender.js:190:20)
file:///home/alok/exp/test-svelte/realworld/node_modules/@sveltejs/kit/src/core/prerender/prerender.js:36
                                throw new Error(format(details));
                                      ^

Error: 500 /
    at file:///home/alok/exp/test-svelte/realworld/node_modules/@sveltejs/kit/src/core/prerender/prerender.js:36:11
    at save (file:///home/alok/exp/test-svelte/realworld/node_modules/@sveltejs/kit/src/core/prerender/prerender.js:349:4)
    at visit (file:///home/alok/exp/test-svelte/realworld/node_modules/@sveltejs/kit/src/core/prerender/prerender.js:199:3)

Node.js v18.12.1
[vite-plugin-sveltekit-compile] Prerendering failed with code 1
error during build:
Error: Prerendering failed with code 1                                                                                                                                                          
    at ChildProcess.<anonymous> (file:///home/alok/exp/test-svelte/realworld/node_modules/@sveltejs/kit/src/exports/vite/index.js:551:15)                                                       
    at ChildProcess.emit (node:events:513:28)                                                                                                                                                   
    at ChildProcess._handle.onexit (node:internal/child_process:291:12)                                                                                                                         
alok@alok-HP-Laptop-14s-cf3xxx:~/exp/test-svelte/realworld$

What changes should I make to make it work?

Advertisement

Answer

The Realworld example is not really a good candidate for static site generation. Parts of it is hidden behind a login, and parts of it makes use of url.searchParams like your error suggests.

You could prerender parts of the site that fit all the criteria and server-side render the rest at runtime.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement