【发布时间】:2021-03-24 23:12:47
【问题描述】:
我正在使用Gatsby,我想使用多语言创建一个站点,到目前为止,我已经定义了pages/index.js,其中包含以下内容:
import React from "react"
import Layout from "../components/layout/layout"
import BGTState from "../context/bgt/bgtState"
import { Router } from "@reach/router"
import Home from "../components/pages/home"
import Collection from "../components/pages/collection"
import NotFound from "../components/pages/404"
const IndexPage = () => {
return (
<BGTState>
<Layout>
<Router>
<Home path="/" />
<Collection path="collection/:id" />
<NotFound default />
</Router>
</Layout>
</BGTState>
)
}
export default IndexPage
我已将gatsby-node.js 修改为:
// Implement the Gatsby API onCreatePage. This is
// called after every page is created.
exports.onCreatePage = async ({ page, actions }) => {
const { createPage } = actions
if (page.path === "/") {
page.matchPath = "/*"
createPage(page)
}
}
每个请求都转发到index.js,但是有问题。我正在使用插件gatsby-plugin-intl,它会在 url 中添加一个动态前缀,例如:http://localhost:3001/en/
如果我访问http://localhost:3001/en/,则会显示NotFound 组件,因为没有与url 匹配的路由。有没有办法为 url 添加前缀并将所有内容重新路由到正确的组件?
【问题讨论】:
标签: javascript reactjs graphql gatsby gatsby-plugin