简体   繁体   English

Sapper/Svelte.js - 如何指定客户端资产位置?

[英]Sapper/Svelte.js - How to specify client-side assets location?

I have a Sapper.js application that I have successfully running on AWS Lambda.我有一个在 AWS Lambda 上成功运行的 Sapper.js 应用程序。 Lambda is able to deliver the server-side generated HTML created by Sapper to AWS API Gateway which then serves the app to the user. Lambda 能够将 Sapper 创建的服务器端生成的 HTML 传送到 AWS API Gateway,然后 AWS API Gateway 将应用程序提供给用户。 I am using S3 to host the client side assets (scripts, webpack chunks, etc).我使用 S3 来托管客户端资产(脚本、webpack 块等)。 The S3 bucket is on a different domain than API Gateway. S3 存储桶与 API Gateway 位于不同的域中。

The issue I'm having is that I need to set an asset prefix for these scripts so that Sapper can find them.我遇到的问题是我需要为这些脚本设置资产前缀,以便 Sapper 可以找到它们。 Currently all of my client side scripts include relative links and look like this: <script src="/client/be33a1fe9c8bbaa6fa9d/SCRIPT_NAME.js"></script> I need to have them look like this: <script src="https://AWS_S3_BUCKET_ENDPOINT.com/client/be33a1fe9c8bbaa6fa9d/SCRIPT_NAME.js"></script>目前我所有的客户端脚本都包含相关链接,如下所示: <script src="/client/be33a1fe9c8bbaa6fa9d/SCRIPT_NAME.js"></script>我需要让它们看起来像这样: <script src="https://AWS_S3_BUCKET_ENDPOINT.com/client/be33a1fe9c8bbaa6fa9d/SCRIPT_NAME.js"></script>

Looking in the Sapper docs , I see that I can specify a base url for the client and server.查看Sapper 文档,我看到我可以为客户端和服务器指定一个基本 URL。 However, changing this base url breaks my app and causes the Lambda rendering the pages to return 404 errors.但是,更改此基本 url 会破坏我的应用程序并导致呈现页面的 Lambda 返回404错误。

I know that when using, say, Next.js, I can accomplish this by modifying the next.config.js file to include the following:我知道在使用 Next.js 时,我可以通过修改next.config.js文件以包含以下内容来完成此操作:

module.exports = {
  assetPrefix: "https://AWS_S3_BUCKET_ENDPOINT.com/client",
}

But I don't know how to do this in Sapper.但我不知道如何在 Sapper 中做到这一点。 Do I need to modify the bundler (using webpack) config?我需要修改打包器(使用 webpack)配置吗? Or is there some other way?或者有其他方法吗?

Thank you.谢谢你。

I think I've figured it out.我想我已经想通了。

I had to change two sapper files.我不得不更改两个工兵文件。 First I went into sapper/dist/webpack.js and modified it like so:首先我进入sapper/dist/webpack.js并像这样修改它:

'use strict';

var __chunk_3 = require('./chunk3.js');

var webpack = {
    dev: __chunk_3.dev,

    client: {
        entry: () => {
            return {
                main: `${__chunk_3.src}/client`
            };
        },

        output: () => {
            return {
                path: `${__chunk_3.dest}/client`,
                filename: '[hash]/[name].js',
                chunkFilename: '[hash]/[name].[id].js',
                // change this line to point to the s3 bucket client key
                publicPath: "https://AWS_S3_BUCKET_ENDPOINT.com/client"
            };
        }
    },

    server: {
        entry: () => {
            return {
                server: `${__chunk_3.src}/server`
            };
        },

        output: () => {
            return {
                path: `${__chunk_3.dest}/server`,
                filename: '[name].js',
                chunkFilename: '[hash]/[name].[id].js',
                libraryTarget: 'commonjs2'
            };
        }
    },

    serviceworker: {
        entry: () => {
            return {
                'service-worker': `${__chunk_3.src}/service-worker`
            };
        },

        output: () => {
            return {
                path: __chunk_3.dest,
                filename: '[name].js',
                chunkFilename: '[name].[id].[hash].js',
                // change this line to point to the s3 bucket root
                publicPath: "https://AWS_S3_BUCKET_ENDPOINT.com"
            }
        }
    }
};

module.exports = webpack;
//# sourceMappingURL=webpack.js.map

Then I had to modify sapper/runtime/server.mjs so that the main variable points to the bucket like so:然后我不得不修改sapper/runtime/server.mjs以便main变量像这样指向存储桶:

...

const main = `https://AWS_S3_BUCKET_ENDPOINT.com/client/${file}`;

...

Testing with the basic sapper webpack template, I can confirm that the scripts are loading from the s3 bucket successully.使用基本的 sapper webpack 模板进行测试,我可以确认脚本正在从 s3 存储桶成功加载。 So far this all looks good.到目前为止,这一切看起来都不错。 I will mess around with the sapper build command next to make it so I can pass these hacks in as command line arguments so I don't have to hardcode them every time.接下来我将使用sapper build命令来实现它,这样我就可以将这些 hack 作为命令行参数传递,这样我就不必每次都对它们进行硬编码。

Now, I'm not sure if this will hold up as the app becomes more complicated.现在,我不确定随着应用程序变得更加复杂,这是否会成立。 Looking into the sapper/runtime/server.mjs file, I see that the req.baseUrl property is referenced in several different locations and I don't know if my hacks will cause any issues with this.查看sapper/runtime/server.mjs文件,我看到req.baseUrl属性在几个不同的位置被引用,我不知道我的黑客是否会导致任何问题。 Or anywhere else in sapper for that matter.或者其他任何地方的工兵。

If anyone with more experience with the Sapper internals is reading, let me know in the comments if I screwed something up 👍如果任何对 Sapper 内部结构有更多经验的人正在阅读,如果我搞砸了什么,请在评论中告诉我👍

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

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