Brandon Kauffman
Brandon Kauffman

Reputation: 1865

Statically deploy SvelteKit to Actix

I'm trying to use actix and SvelteKit. I'm confused on how to build the svelte app. I have multiple routes defined in the svelte app.

My svelte.config.js looks like

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

/** @type {import('@sveltejs/kit').Config} */
const config = {
    // Consult https://github.com/sveltejs/svelte-preprocess
    // for more information about preprocessors
    preprocess: preprocess(),
    trailingSlash: 'always',
    kit: {
        adapter: adapter({
            fallback: 'portfolio.html'
        }),
    }
};

export default config;

And in actix

async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        App::new().service(
            fs::Files::new("/", "/home/bk/rust_dev/portfolio/frontend/portfolio/build")
                .show_files_listing(),
        )
    })
    .bind(("127.0.0.1", 8080))?
    .run()
    .await
}

When navigating to the index built, I get a 404 error inside of the content of the page.

404

Not found: /portfolio.html

ge@http://127.0.0.1:8080/_app/immutable/start-50a437dc.js:1:14076 be@http://127.0.0.1:8080/_app/immutable/start-50a437dc.js:1:20801 Pe@http://127.0.0.1:8080/_app/immutable/start-50a437dc.js:1:13594 goto@http://127.0.0.1:8080/_app/immutable/start-50a437dc.js:1:21319 qt@http://127.0.0.1:8080/_app/immutable/start-50a437dc.js:1:24870 @http://127.0.0.1:8080/portfolio.html:21:8

I'm guessing the source of my error is from improperly building/serving svelte.

Upvotes: 2

Views: 679

Answers (1)

Brandon Kauffman
Brandon Kauffman

Reputation: 1865

You're using SPA mode by specifying fallback. You need to serve each endpoint with index.html.

This is what is looks like in actix:

async fn main() -> std::io::Result<()> {
    let path = "/home/bk/rust_dev/portfolio/frontend/portfolio/build";
    let static_files = String::from(path.strip_suffix('/').unwrap_or(path));
    HttpServer::new(move || {
        App::new().service(
            fs::Files::new("/", static_files.clone())
                .index_file("index.html")
                .default_handler(
                    fs::NamedFile::open(
                        vec![static_files.clone(), "index.html".to_string()].join("/"),
                    )
                    .expect("index file should exist"),
                ),
        )
    })
    .bind(("127.0.0.1", 8080))?
    .run()
    .await
}

Upvotes: 3

Related Questions