我在让 nuxt3 在生产中为 /.well-known/apple-developer-merchantid-domain-association 路径提供服务时遇到问题。我正在尝试使用静态托管(cloudflare),但是当我部署项目
npx nuxt generate
时,路由总是收到 404。我尝试将文件夹/文件添加到公共目录(404),并尝试将文件的内容注册为服务器路由与硝基的预渲染配置相结合,但也没有运气......
令人烦恼的是,如果我使用 miniflare 在本地模拟生产,然后运行
npx nuxt build
& npx nuxt preview
我可以访问该路线。
还注意到 /.output/public/_nuxt/entry-[hash].mjs 文件似乎没有像其他所有资产/页面那样注册路由。
nuxt.config.ts
import { defineNuxtConfig } from 'nuxt'
// https://v3.nuxtjs.org/api/configuration/nuxt.config
export default defineNuxtConfig({
nitro: {
preset: 'cloudflare',
prerender: {
routes: ['/.well-known/apple-developer-merchantid-domain-association']
}
},
ssr: false,
target: 'static'
})
/server/routes/.well-known/apple-developer-merchantid-domain-association.ts
import { defineEventHandler } from 'h3'
export default defineEventHandler(() => "92324FSD2FFADAD124RTSDGSDF7B227073704964223A22")
您可以在
static
目录中托管文件夹/文件,该目录会映射到应用程序/站点的根目录。我刚刚做了类似的事情,并为 Microsoft 托管了一个 .wellknown 文件夹,如下所示 /static/.well-known/microsoft-identity-association.json
。
由于我没有找到 Nuxt 解决方案,因此我最终有条件地覆盖了 Cloudflare 的 Wrangler index.js 文件中的请求。这样它就绕过了Nuxt的entry-[hash].js,而是直接检索资源。
async function handleEvent(event) {
const { pathname } = new URL(event.request.url)
if(pathname === '/.well-known/apple-developer-merchantid-domain-association'){
const response = await getAssetFromKV(event, {
mapRequestToAsset: (req) =>
new Request(`${new URL(req.url).origin}/.well-known/apple-developer-merchantid-domain-association.html`, req),
})
return new Response(response.body, {...response,})
}
//
// rest of Nuxt's routing here
//
}
对于那些在 Nuxt 3 中仍然遇到此问题的人,您可以从 nginx 配置中解决它。
将文件保存在
/var/www/nuxt-directory/public/.well-known/
文件夹中,然后在代理传递之前在 nginx 配置中添加以下代码。
server {
listen 80;
server_name nuxt-site-host;
location ^~ /.well-known/ {
default_type "text/plain";
root /var/www/nuxt-directory/public;
try_files $uri =404;
}
# other server configuration...
}