Skip to content

Commit

Permalink
refactor: Improving public path support
Browse files Browse the repository at this point in the history
  • Loading branch information
scottsut committed Aug 15, 2023
1 parent 61eb69e commit 7ae0fe8
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion frontend/craco.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ module.exports = {
chunkFilename: 'static/js/[name].[contenthash:8].js',
},
// path: path.resolve(__dirname, 'dist'), // 修改输出文件目录
publicPath: '/',
publicPath: `${process.env.PUBLIC_URL || ''}/`,
};
/**
* webpack split chunks
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/app/AppRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import { ConfigProvider, message } from 'antd';
import echartsDefaultTheme from 'app/assets/theme/echarts_default_theme.json';
import { registerTheme } from 'echarts';
import { StorageKeys } from 'globalConstants';
import { PUBLIC_URL, StorageKeys } from 'globalConstants';
import { antdLocales } from 'locales/i18n';
import { useEffect, useLayoutEffect } from 'react';
import { Helmet } from 'react-helmet-async';
Expand Down Expand Up @@ -65,7 +65,7 @@ export function AppRouter() {

return (
<ConfigProvider locale={antdLocales[i18n.language]}>
<BrowserRouter>
<BrowserRouter basename={PUBLIC_URL}>
<Helmet
titleTemplate="%s - Datart"
defaultTitle="Datart"
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/app/pages/DashBoardPage/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import { getTime, splitRangerDateFilters } from 'app/utils/time';
import {
DATE_FORMATTER,
FilterSqlOperator,
PUBLIC_URL,
TIME_FORMATTER,
} from 'globalConstants';
import moment from 'moment';
Expand Down Expand Up @@ -73,7 +74,7 @@ export const dateFormatObj = {

export const convertImageUrl = (urlKey: string = ''): string => {
if (urlKey.startsWith(BOARD_FILE_IMG_PREFIX)) {
return `${window.location.origin}/${urlKey}`;
return `${window.location.origin}${PUBLIC_URL}/${urlKey}`;
}
return urlKey;
};
Expand Down
11 changes: 5 additions & 6 deletions frontend/src/app/pages/SharePage/Chart/Router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import { ConfigProvider } from 'antd';
import echartsDefaultTheme from 'app/assets/theme/echarts_default_theme.json';
import { registerTheme } from 'echarts';
import { PUBLIC_URL } from 'globalConstants';
import { antdLocales } from 'locales/i18n';
import { useTranslation } from 'react-i18next';
import { BrowserRouter, Route, Switch } from 'react-router-dom';
Expand All @@ -33,13 +34,11 @@ export function Router() {

return (
<ConfigProvider locale={antdLocales[i18n.language]}>
<BrowserRouter>
<BrowserRouter basename={PUBLIC_URL}>
<HelmetPageTitle lang={i18n.language} />
<BrowserRouter>
<Switch>
<Route path="/shareChart/:token" component={LazyShareChart} />
</Switch>
</BrowserRouter>
<Switch>
<Route path="/shareChart/:token" component={LazyShareChart} />
</Switch>
<GlobalStyles />
</BrowserRouter>
</ConfigProvider>
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/app/pages/SharePage/Dashboard/Router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import { ConfigProvider } from 'antd';
import echartsDefaultTheme from 'app/assets/theme/echarts_default_theme.json';
import { registerTheme } from 'echarts';
import { PUBLIC_URL } from 'globalConstants';
import { antdLocales } from 'locales/i18n';
import { useTranslation } from 'react-i18next';
import { BrowserRouter, Route, Switch } from 'react-router-dom';
Expand All @@ -33,7 +34,7 @@ export function Router() {

return (
<ConfigProvider locale={antdLocales[i18n.language]}>
<BrowserRouter>
<BrowserRouter basename={PUBLIC_URL}>
<HelmetPageTitle lang={i18n.language} />
<Switch>
<Route path="/shareDashboard/:token" component={LazyShareDashboard} />
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/app/pages/SharePage/StoryPlayer/Router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import { ConfigProvider } from 'antd';
import echartsDefaultTheme from 'app/assets/theme/echarts_default_theme.json';
import { registerTheme } from 'echarts';
import { PUBLIC_URL } from 'globalConstants';
import { antdLocales } from 'locales/i18n';
import { useTranslation } from 'react-i18next';
import { BrowserRouter, Route, Switch } from 'react-router-dom';
Expand All @@ -33,7 +34,7 @@ export function Router() {

return (
<ConfigProvider locale={antdLocales[i18n.language]}>
<BrowserRouter>
<BrowserRouter basename={PUBLIC_URL}>
<HelmetPageTitle lang={i18n.language} />
<Switch>
<Route
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/app/utils/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
transformToViewConfig,
} from 'app/utils/internalChartHelper';
import { saveAs } from 'file-saver';
import { PUBLIC_URL } from 'globalConstants';
import i18next from 'i18next';
import qs from 'qs';
import { request2, requestWithHeader } from 'utils/request';
Expand Down Expand Up @@ -250,7 +251,7 @@ export async function downloadFile(id) {

export async function fetchPluginChart(path) {
const result = await request2(path, {
baseURL: '/',
baseURL: PUBLIC_URL,
headers: { Accept: 'application/javascript' },
}).catch(error => {
console.error(error);
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/globalConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ export enum StorageKeys {
Theme = 'THEME',
}

export const BASE_API_URL = '/api/v1';
export const BASE_RESOURCE_URL = '/';
export const PUBLIC_URL = process?.env?.PUBLIC_URL || '';
export const BASE_API_URL = `${PUBLIC_URL}/api/v1`;
export const BASE_RESOURCE_URL = `${PUBLIC_URL}/`;
// 1 hour
export const DEFAULT_AUTHORIZATION_TOKEN_EXPIRATION = 1000 * 60 * 60;

Expand Down
4 changes: 2 additions & 2 deletions frontend/src/utils/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import { message } from 'antd';
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';
import { BASE_API_URL } from 'globalConstants';
import { BASE_API_URL, PUBLIC_URL } from 'globalConstants';
import i18next from 'i18next';
import { APIResponse } from 'types';
import { getToken, removeToken, setToken } from './auth';
Expand Down Expand Up @@ -93,7 +93,7 @@ export function requestWithHeader(
}

export const getServerDomain = () => {
return `${window.location.protocol}//${window.location.host}`;
return `${window.location.protocol}//${window.location.host}${PUBLIC_URL}`;
};

function unAuthorizationErrorHandler(error) {
Expand Down

0 comments on commit 7ae0fe8

Please sign in to comment.