Skip to content

Commit c0b73c4

Browse files
committed
BN-35 | Refactor. Set basename value for router based on build env
1 parent bbc7232 commit c0b73c4

File tree

2 files changed

+5
-7
lines changed

2 files changed

+5
-7
lines changed

src/index.tsx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ import { BrowserRouter } from 'react-router-dom';
44
import App from './App';
55
import './styles/index.scss';
66

7-
const getBasename = () => {
8-
const pathname = window.location.pathname;
9-
const basePath = pathname.split('/').slice(0, 2).join('/');
10-
return basePath || '/';
11-
};
12-
137
// Register service worker for PWA
148
if ('serviceWorker' in navigator) {
159
window.addEventListener('load', () => {
@@ -34,7 +28,7 @@ const root = createRoot(container);
3428

3529
root.render(
3630
<React.StrictMode>
37-
<BrowserRouter basename={getBasename()}>
31+
<BrowserRouter basename={process.env.PUBLIC_URL || '/'}>
3832
<App />
3933
</BrowserRouter>
4034
</React.StrictMode>,

webpack.config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const path = require('path');
2+
const webpack = require('webpack');
23
const HtmlWebpackPlugin = require('html-webpack-plugin');
34
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
45
const CopyWebpackPlugin = require('copy-webpack-plugin');
@@ -73,6 +74,9 @@ module.exports = (env, argv) => {
7374
],
7475
},
7576
plugins: [
77+
new webpack.DefinePlugin({
78+
'process.env.PUBLIC_URL': JSON.stringify(publicPath)
79+
}),
7680
new HtmlWebpackPlugin({
7781
template: './public/index.html',
7882
favicon: './public/favicon.ico',

0 commit comments

Comments
 (0)