简体   繁体   English

SvelteKit.next 静态站点适配器页面在刷新时返回 404

[英]SvelteKit.next Static Site Adapter Pages Return 404 on Refresh

I know this can't be that hard, but Googling isn't turning up any answers.我知道这不会那么难,但谷歌搜索没有找到任何答案。 I'm using the SvelteKit static site adapter, and it's building the site, but the build file structure is:我正在使用 SvelteKit 静态站点适配器,它正在构建站点,但构建文件结构是:

root
  - _app
  - faviceon.png
  - index.html
  - secondpage.html

But, when you click on the navigation items to take you to the secondary page, the URL is: url.com/secondpage rather than url.com/secondpage.html .但是,当您单击导航项将您带到二级页面时,URL 是: url.com/secondpage而不是url.com/secondpage.html I'm assuming the generated JS is doing the routing here, but if you input the url directly or refresh the second page, it returns a 404.我假设生成的 JS 在这里做路由,但是如果你直接输入 url 或刷新第二页,它会返回 404。

I know this has to be a config issue, but I've tried tons of variations, and the build stays the same.我知道这一定是一个配置问题,但我已经尝试了很多变体,并且构建保持不变。 This is my svelte.config.js file:这是我的svelte.config.js文件:

// import adapter from "@sveltejs/adapter-auto";
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({
        scss: {
            prependData: `@import './src/lib/scss/style.scss';`,
        },
    }),

    kit: {
        adapter: adapter({
            // default options are shown. On some platforms
            // these options are set automatically — see below
            pages: "build",
            assets: "build",
            fallback: null,
            precompress: false,
            trailingSlash: 'always',
        }),
    },
};

export default config;

I initially didn't have the trailingSlash param, but the few sort of related things in docs and on StackOverflow made it seem like that might affect the routing code generation.我最初没有 trailingSlash 参数,但文档和 StackOverflow 上的一些相关内容看起来可能会影响路由代码生成。

Anyway, let me know if you need any other code samples from me, or if just not seeing the forest for the trees.不管怎样,如果您需要我提供任何其他代码示例,或者只是只见树木不见森林,请告诉我。

Thanks!谢谢!

I found the same situation, I found out that I should put a line in src/routes/+layout.js我发现了同样的情况,我发现我应该在 src/routes/+layout.js 中放一行

export const trailingSlash = 'always'

The problem is that when there are multiple pages, the adapter-static generates several html files and expects all links to be static.问题是,当有多个页面时,静态适配器会生成多个 html 文件,并期望所有链接都是静态的。 The routes, even when they are static, are being treated by static-adapter as dynamic and it complains when prerender: true is set.这些路由,即使它们是静态的,也会被静态适配器视为动态的,并且当设置了prerender: true时它会抱怨。

The adapter-static has two distinct modes of operation: SPA and prerendering.适配器静态有两种不同的操作模式:SPA 和预渲染。 When there are several routes, both the npm run dev and npm run preview works as intended, but once built, the static routing falls to the web server who cannot found the page without the .html .当有多个路由时, npm run devnpm run preview都按预期工作,但是一旦构建,静态路由就会落到 Web 服务器上,如果没有.html就无法找到页面。

I found a workaround to avoid converting the site to a SPA: Installing a url rewrite mechanism as a middleware in order to add the .html extension that the static server is expecting in the compiled site.我找到了避免将站点转换为 SPA 的解决方法:安装 url 重写机制作为中间件,以便在编译后的站点中添加静态服务器期望的.html扩展名。

For Nginx static server the solution is a url rewrite configuration as explained here: https://www.codesmite.com/article/clean-url-rewrites-using-nginx For other servers, the solution should be very similar对于 Nginx 静态服务器,解决方案是 url 重写配置,如下所述: https ://www.codesmite.com/article/clean-url-rewrites-using-nginx 对于其他服务器,解决方案应该非常相似

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM