【问题标题】:How do I build my Svelte website with all relative paths?如何使用所有相对路径构建我的 Svelte 网站?
【发布时间】:2022-08-11 23:49:45
【问题描述】:

我的网站包含不同的“项目”,每个项目都链接到自己的 Git 存储库并从 Netlify 构建。例如,如果我的网站是 www.thedivtagguy.com,www.thedivtagguy.com/project1 和 www.thedivtagguy.com/project2 是两个不同的网站,但作为子目录链接到主域(而不是 project1.thedivtagguy.com)。

一个这样的示例站点是可用的here。如您所见,它正确呈现了所有样式。但是,如果我通过主站点this is what happens 的重定向访问它。没有样式。

对于上述情况,我只是设置了一个重定向规则,将/testing/ 链接到https://thedivtagguy2.netlify.app。

这是我运行 npm build 时 public 目录的样子:

????public
 ┣ ????api
 ┣ ????assets
 ┣ ????common
 ┣ ????app.css
 ┗ ????index.html

即使在本地运行,index.html 也不会呈现正确的样式。这是 index.html 的 <head> 标记中包含的内容:


        <link rel=\"modulepreload\" href=\"/_app/start-428f887f.js\">
        <link rel=\"modulepreload\" href=\"/_app/chunks/vendor-28205438.js\">
        <link rel=\"modulepreload\" href=\"/_app/pages/__layout.svelte-da7e95f6.js\">
        <link rel=\"modulepreload\" href=\"/_app/pages/index.svelte-33b339ff.js\">
        <link rel=\"modulepreload\" href=\"/_app/chunks/Meta-7977247e.js\">
        <link rel=\"stylesheet\" href=\"/_app/assets/start-c446e5f0.css\">
        <link rel=\"stylesheet\" href=\"/_app/assets/pages/__layout.svelte-aa99de20.css\">
        <link rel=\"stylesheet\" href=\"/_app/assets/pages/index.svelte-e673c7ca.css\">

        <script type=\"module\">
            import { start } from \"/_app/start-428f887f.js\";
            start({
                target: document.querySelector(\"#svelte\"),
                paths: {\"base\":\"\",\"assets\":\"\"},
                session: {},
                host: location.host,
                route: true,
                spa: false,
                trailing_slash: \"never\",
                hydrate: {
                    status: 200,
                    error: null,
                    nodes: [
                        import(\"/_app/pages/__layout.svelte-da7e95f6.js\"),
                        import(\"/_app/pages/index.svelte-33b339ff.js\")
                    ],
                    page: {
                        host: location.host, // TODO this is redundant
                        path: \"\\u002F\",
                        query: new URLSearchParams(\"\"),
                        params: {}
                    }
                }
            });
        </script>

如您所见,它似乎为样式、图像和 JS 包引用了一些不同的路径。

我的直觉猜测是我需要为我的构建设置正确的相对路径,以便它在本地构建和通过重定向访问时工作。

我怎么做?如何解决此问题,以便正确呈现我的样式和其他资产?

我的存储库是here。

  • 你知道了吗?

标签: javascript html bundle svelte


【解决方案1】:

在vite.config.ts(如果你不使用TypeScript,可能是vite.config.js)中,将base: './' 添加到配置中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-10
    • 1970-01-01
    • 2020-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-28
    相关资源
    最近更新 更多