admin管理员组文章数量:1287607
I'm using Create-react-app with npm start
mand to serve my application in the development server. By default the application is served from https://localhost:3000/. However my application uses cookies which requires specific context path. How do I serve the application from https://localhost:3000/app/ instead?
I'm using Create-react-app with npm start
mand to serve my application in the development server. By default the application is served from https://localhost:3000/. However my application uses cookies which requires specific context path. How do I serve the application from https://localhost:3000/app/ instead?
3 Answers
Reset to default 3You have a few options here.
Production Mode
Set the environment variable PUBLIC_PATH
to /app/
or
As mentioned in the other answer, use homepage
field in package.json
Development Mode
The config is more of hardcoded into the app. You need to eject
the app first to make any edits.
Step 1
npm run eject
Step 2
In config/webpack.config.js
, Find the below section (Somewhere around line 67 - 68)
const publicPath = isEnvProduction
? paths.servedPath
: isEnvDevelopment && '/';
and change to
const publicPath = isEnvProduction
? paths.servedPath
: isEnvDevelopment && '/app/';
Step 3
In config/webpackDevServer.config.js
, find the below section (Somewhere around line 60 - 65)
// It is important to tell WebpackDevServer to use the same "root" path
// as we specified in the config. In development, we always serve from /.
publicPath: '/',
And change to
publicPath: '/app',
Step 4
npm start
You can specify the path by adding: "homepage": "http://mywebsite./relativepath"
to your package.json file according to the documentation found here:
https://create-react-app.dev/docs/deployment/#building-for-relative-paths
I've already solved this problem with:
<BrowserRouter basename="/app" />
<Link to="/test" />
With this solution, that Link
path is /app/test
.
Another example:
<BrowserRouter basename="/app">
<Routes>
<Route element={<Home />} path="/" exact />
<Route element={<Test />} path="/test" />
</Routes>
</BrowserRouter>
本文标签: javascriptCreatereactapp how to define contextpath of development serverStack Overflow
版权声明:本文标题:javascript - Create-react-app: how to define context-path of development server - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741313821a2371797.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论