6

I'm using Create-react-app with npm start command 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 3

3

You 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
Sign up to request clarification or add additional context in comments.

1 Comment

The environment variable pointed in the answer does not work. I found the PUBLIC_URL in the tool docs and it seems to do the job.
3

You can specify the path by adding: "homepage": "http://mywebsite.com/relativepath" to your package.json file according to the documentation found here:

https://create-react-app.dev/docs/deployment/#building-for-relative-paths

Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.