如何使用 .htaccess 的 404 页面回退在 Nuxt 路由器中手动生成页面
How to manually generate pages in Nuxt router with a 404 page fallback for .htaccess
我正在尝试使用 Nuxt.js 创建一个 SSG 站点。
当我访问未在 nuxt.config.js 的生成 属性 中设置的路由时,
我想在不更改 URL 的情况下显示 404 页面的内容。(使用 htaccess)
以下是正在建设中的网站
http://we-are-sober.d3v-svr.com/xxxx
这按预期工作。
http://we-are-sober.d3v-svr.com/user/xxxx
这没有按预期工作。
显示了一会404页面的内容,但很快就执行了基于“user/_id.vue”动态路由的流程
问题的关键是不存在的路由表现得好像存在一样。
有谁知道如何解决这个问题?
这是源代码。
https://github.com/yhirochick/rewrite_test
404.vue
https://github.com/yhirochick/rewrite_test/blob/master/pages/404.vue
user/_id.vue
https://github.com/yhirochick/rewrite_test/blob/master/pages/user/_id.vue
nuxt.config.js
https://github.com/yhirochick/rewrite_test/blob/master/nuxt.config.js#L43-L45
.htaccess
https://github.com/yhirochick/rewrite_test/blob/master/static/.htaccess
我是日本人。以上文字根据Google翻译。
可能比较难理解,谢谢。
我处理此类问题同时最大限度地减少所需 API 调用的方法遵循以下步骤:
- 生成一个全新的 Nuxt 项目
- 安装 axios:
yarn add -D axios
- 将此添加到
nuxt.config.js
文件
import axios from 'axios'
export default {
...
generate: {
routes: async () => {
const users = await axios.get('https://jsonplaceholder.typicode.com/users')
return users.data.map((user) => ({
route: `/users/${user.id}`,
payload: user,
}))
},
fallback: 'no-user.html', // this one is not needed anymore if you ditch the redirect!
},
}
这将生成所有需要的路由,同时由于 payload
将调用保持在最低限度,稍后将传递到页面。可以在 the docs.
中找到更多信息
- 然后,创建
/pages/users/_id.vue
页面就可以了
<template>
<div>
<div v-if="user">User name: {{ user.name }}</div>
<div v-else-if="error">{{ error }}</div>
</div>
</template>
<script>
export default {
asyncData({ payload }) {
if (payload && Object.entries(payload).length) return { user: payload }
else return { error: 'This user does not exist' } // this will also catch users going to `/users/`
},
}
</script>
- 创建一些
no-user.vue
页面,error.vue
布局,你应该是 gucci
最后,我们有 10 个来自模拟 API 的用户。所以这些是以下情况:
- 如果我们转到
/users/5
,用户已经是静态的,所以我们确实有它的信息,不需要任何额外的 API 调用
- 如果我们转到
/users/11
,构建时用户不在场,因此他不在这里,我们显示错误
- 如果我们转到
/users
,我们仍然会被发送到/pages/users/_id
页面,但是由于:id
在那里是可选的,它会出错并仍然显示错误,一个 index.vue
当然可以处理这种情况
我的 github 存储库可以在这里找到:https://github.com/kissu/so-nuxt-generate-placeholder-users
这是一个棘手的方法,但我找到了我想要的代码。
https://fes.d3v-svr.com/users/xxxxxx
是我期待的作品。
- 用户 xxxxxx 不存在
- 显示404页面
- 原样URL/users/xxxxxx
首先,只需设置.htaccess 将不存在的页面重写为404页面
ErrorDocument 404 /no-user/index.html
仅在上面,Nuxt 执行基于 URL /users/xxxxxx/
并将页面重新呈现为“UserXXXXXX”,即使他不存在。
为了避免这种情况,users/_id.vue
如下所示。
模板
<template>
<div v-if="ssr">User name: {{ user.name }}</div>
</template>
脚本
<script>
export default {
asyncData({ payload }) {
return { user: payload, ssr:true }
},
}
</script>
好像是如果模板为空,nuxt不会执行脚本依赖URL.
这非常棘手,所以我将继续介绍它的工作原理。
我正在尝试使用 Nuxt.js 创建一个 SSG 站点。 当我访问未在 nuxt.config.js 的生成 属性 中设置的路由时, 我想在不更改 URL 的情况下显示 404 页面的内容。(使用 htaccess)
以下是正在建设中的网站
http://we-are-sober.d3v-svr.com/xxxx
这按预期工作。
http://we-are-sober.d3v-svr.com/user/xxxx
这没有按预期工作。
显示了一会404页面的内容,但很快就执行了基于“user/_id.vue”动态路由的流程
问题的关键是不存在的路由表现得好像存在一样。 有谁知道如何解决这个问题?
这是源代码。 https://github.com/yhirochick/rewrite_test
404.vue
https://github.com/yhirochick/rewrite_test/blob/master/pages/404.vue
user/_id.vue
https://github.com/yhirochick/rewrite_test/blob/master/pages/user/_id.vue
nuxt.config.js
https://github.com/yhirochick/rewrite_test/blob/master/nuxt.config.js#L43-L45
.htaccess
https://github.com/yhirochick/rewrite_test/blob/master/static/.htaccess
我是日本人。以上文字根据Google翻译。 可能比较难理解,谢谢。
我处理此类问题同时最大限度地减少所需 API 调用的方法遵循以下步骤:
- 生成一个全新的 Nuxt 项目
- 安装 axios:
yarn add -D axios
- 将此添加到
nuxt.config.js
文件
import axios from 'axios'
export default {
...
generate: {
routes: async () => {
const users = await axios.get('https://jsonplaceholder.typicode.com/users')
return users.data.map((user) => ({
route: `/users/${user.id}`,
payload: user,
}))
},
fallback: 'no-user.html', // this one is not needed anymore if you ditch the redirect!
},
}
这将生成所有需要的路由,同时由于 payload
将调用保持在最低限度,稍后将传递到页面。可以在 the docs.
- 然后,创建
/pages/users/_id.vue
页面就可以了
<template>
<div>
<div v-if="user">User name: {{ user.name }}</div>
<div v-else-if="error">{{ error }}</div>
</div>
</template>
<script>
export default {
asyncData({ payload }) {
if (payload && Object.entries(payload).length) return { user: payload }
else return { error: 'This user does not exist' } // this will also catch users going to `/users/`
},
}
</script>
- 创建一些
no-user.vue
页面,error.vue
布局,你应该是 gucci
最后,我们有 10 个来自模拟 API 的用户。所以这些是以下情况:
- 如果我们转到
/users/5
,用户已经是静态的,所以我们确实有它的信息,不需要任何额外的 API 调用 - 如果我们转到
/users/11
,构建时用户不在场,因此他不在这里,我们显示错误 - 如果我们转到
/users
,我们仍然会被发送到/pages/users/_id
页面,但是由于:id
在那里是可选的,它会出错并仍然显示错误,一个index.vue
当然可以处理这种情况
我的 github 存储库可以在这里找到:https://github.com/kissu/so-nuxt-generate-placeholder-users
这是一个棘手的方法,但我找到了我想要的代码。
https://fes.d3v-svr.com/users/xxxxxx 是我期待的作品。
- 用户 xxxxxx 不存在
- 显示404页面
- 原样URL/users/xxxxxx
首先,只需设置.htaccess 将不存在的页面重写为404页面
ErrorDocument 404 /no-user/index.html
仅在上面,Nuxt 执行基于 URL /users/xxxxxx/
并将页面重新呈现为“UserXXXXXX”,即使他不存在。
为了避免这种情况,users/_id.vue
如下所示。
模板
<template>
<div v-if="ssr">User name: {{ user.name }}</div>
</template>
脚本
<script>
export default {
asyncData({ payload }) {
return { user: payload, ssr:true }
},
}
</script>
好像是如果模板为空,nuxt不会执行脚本依赖URL.
这非常棘手,所以我将继续介绍它的工作原理。