[英]Serve non-generated files on static site
I am deploying a statically generated site using Nuxt on Cloudflare workers.我正在 Cloudflare 工作人员上使用 Nuxt 部署静态生成的站点。 It works great so far.到目前为止效果很好。
I generate most of my static files on generate, however, there are scenarios where I cannot generate a file, for instance, when an order has been made, I cannot generate the /order/82781
route.我在generate上生成了大部分静态文件,但是,有些情况下我无法生成文件,例如,下订单后,我无法生成/order/82781
路由。 How can I use .htaccess to serve the, I guess index.html file?, if it doesn't exist, so vue/nuxt will take over, and send a request to find the order?我如何使用 .htaccess 来提供 index.html 文件?,如果它不存在,那么 vue/nuxt 将接管,并发送请求以查找订单?
Location is: /my-account/orders/{some_id}位置是:/my-account/orders/{some_id}
I have tried with this htaccess inside /my-account/orders directory:我在 /my-account/orders 目录中尝试过这个 htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
But that doesn't work.但这不起作用。 If I visit the URL directly I get this error:如果我直接访问 URL,则会收到此错误:
could not find my-account/orders/wc_order_sp8DykeTNqMMO/index.html in your content namespace
It sounds like there are parts of your site that are not totally static.听起来您网站的某些部分并非完全静态。 An "htaccess" file gives directions to Apache web server to handle requests dynamically, but you are not using Apache, you are using Workers. “htaccess”文件指示 Apache Web 服务器动态处理请求,但您使用的不是 Apache,而是 Workers。 In Workers, we "configure" the web server by writing JavaScript.在 Workers,我们通过编写 JavaScript 来“配置”Web 服务器。 The good news is that unlike htaccess -- which is restricted to a narrow feature set -- in JavaScript, you can do just about anything!好消息是,与 htaccess 不同——它仅限于一个狭窄的功能集——在 JavaScript 中,你可以做任何事情!
When you generated your Workers Sites project with wrangler generate
, it created a file called workers-site/index.js
.当您使用wrangler generate
生成 Workers Sites 项目时,它创建了一个名为workers-site/index.js
的文件。 This file was copied from a template , but you are free to edit it to add dynamic features to your site.此文件是从模板复制而来,但您可以自由编辑它以向您的站点添加动态功能。
It sounds like what you want to do is make sure /my-account/orders/*
always serves the same HTML file.听起来您想要做的是确保/my-account/orders/*
始终提供相同的 HTML 文件。 Let's program that in JavaScript.让我们用 JavaScript 编写它。
In your index.js
, you'll see a comment like this:在你的index.js
,你会看到这样的评论:
/**
* You can add custom logic to how we fetch your assets
* by configuring the function `mapRequestToAsset`
*/
// options.mapRequestToAsset = handlePrefix(/^\/docs/)
This handlePrefix
function is defined at the bottom of the file .这个handlePrefix
函数定义在文件的底部。 It's actually there as an example.它实际上就是一个例子。 The example code modifies path names by stripping off a prefix.示例代码通过去除前缀来修改路径名。
You want to do something similar but not quite the same: You want to match paths starting with /my-account/orders/
, and then you want to remove the rest.您想要做一些类似但不完全相同的事情:您想要匹配以/my-account/orders/
开头的路径,然后您想要删除其余的路径。 So you could write a function like this:所以你可以写一个这样的函数:
function stripOrderNumber(prefix) {
return request => {
let url = new URL(request.url)
if (url.pathname.startsWith("/my-account/orders/")) {
// Treat as if the order number weren't given.
url.pathname = "/my-account/orders/"
// Update the Request object with the modified URL.
request = new Request(url, request)
}
// Invoke default handling from here.
// (`mapRequestToAsset` is imported from '@cloudflare/kv-asset-handler'
// at the top of this file.)
return mapRequestToAsset(request)
}
}
Now go back up to the commented line and change it to:现在回到注释行并将其更改为:
/**
* You can add custom logic to how we fetch your assets
* by configuring the function `mapRequestToAsset`
*/
options.mapRequestToAsset = stripOrderNumber
Deploy your updated worker, and now /my-account/orders/*
should be mapped to /my-account/orders/index.html
!部署更新后的工作人员,现在/my-account/orders/*
应该映射到/my-account/orders/index.html
!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.