Skip to content

Commit 1e10f51

Browse files
authored
fix: web-server local start axios base url (apitable#155)
Submit a pull request for this project. <!-- If you have an Issue that related to this Pull Request, you can copy this Issue's description --> # Why? <!-- > Related to which issue? > Why we need this pull request? > What is the user story for this pull request? --> # What? <!-- > Can you describe this feature in detail? > Who can benefit from it? --> # How? <!-- > Do you have a simple description of how this pull request is implemented? -->
1 parent 31d0fb1 commit 1e10f51

File tree

5 files changed

+18
-13
lines changed

5 files changed

+18
-13
lines changed

packages/datasheet/pages/index.tsx

+2-4
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,20 @@
1616
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

19-
import { Url } from '@apitable/core';
2019
import axios from 'axios';
2120
import { NextPageContext } from 'next';
21+
import { getBaseUrl } from '../utils/get_base_url';
2222

2323
const App = () => {
2424
return null;
2525
};
2626

2727
export const getServerSideProps = async(context: NextPageContext) => {
28-
const host = process.env.API_PROXY || 'http://localhost:3000';
29-
axios.defaults.baseURL = host + Url.BASE_URL;
28+
axios.defaults.baseURL = getBaseUrl(context);
3029

3130
if (!context.req?.url) {
3231
return { props: {}};
3332
}
34-
3533
const cookie = context.req?.headers.cookie;
3634
const headers: Record<string, string> = {};
3735

packages/datasheet/pages/share/[...all].tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@
1616
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

19-
import { Api, Url } from '@apitable/core';
19+
import { Api } from '@apitable/core';
2020
import axios from 'axios';
2121
import { NextPageContext } from 'next';
2222
import dynamic from 'next/dynamic';
2323
import { getRegResult, shareIdReg } from 'pc/hooks';
2424
import React from 'react';
25+
import { getBaseUrl } from '../../utils/get_base_url';
2526

2627
const DynamicComponentWithNoSSR = dynamic(() => import('../../src/pc/components/share/share'), { ssr: false });
2728

@@ -32,8 +33,7 @@ const App = (props) => {
3233
};
3334

3435
export const getServerSideProps = async(context: NextPageContext) => {
35-
const host = process.env.API_PROXY;
36-
axios.defaults.baseURL = host + Url.BASE_URL;
36+
axios.defaults.baseURL = getBaseUrl(context);
3737

3838
if (!context.req?.url) {
3939
return { props: {}};

packages/datasheet/pages/template/[category_code]/index.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

19-
import { Api, ConfigConstant, Url } from '@apitable/core';
19+
import { Api, ConfigConstant } from '@apitable/core';
2020
import axios from 'axios';
2121
import { TemplateListContext } from 'context/template_list';
2222
import { TemplateRecommendContext } from 'context/template_recommend';
2323
import { NextPageContext } from 'next';
2424
import dynamic from 'next/dynamic';
2525
import React from 'react';
2626
import { getRequestHeaders } from '../../../utils/utils';
27+
import { getBaseUrl } from '../../../utils/get_base_url';
2728

2829
const TemplateCentre = dynamic(() => import('pc/components/template_centre/template_centre'), { ssr: false });
2930
const TemplatePreview = dynamic(() => import('pc/components/template_centre/template_preview'), { ssr: true });
@@ -42,7 +43,6 @@ const App = (props) => {
4243

4344
export const getServerSideProps = async(context: NextPageContext) => {
4445
const { category_code: categoryCode } = context['params'];
45-
const host = process.env.API_PROXY;
4646

4747
if (categoryCode === 'album') {
4848
return { props: {}};
@@ -57,7 +57,7 @@ export const getServerSideProps = async(context: NextPageContext) => {
5757
return { props: {}};
5858
}
5959

60-
axios.defaults.baseURL = host + Url.BASE_URL;
60+
axios.defaults.baseURL = getBaseUrl(context);
6161
const headers = getRequestHeaders(context);
6262

6363
const userMeRes = await Api.getUserMe({}, false, headers);

packages/datasheet/pages/template/index.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1717
*/
1818

19-
import { Api, Url } from '@apitable/core';
19+
import { Api } from '@apitable/core';
2020
import axios from 'axios';
2121
import { TemplateRecommendContext } from 'context/template_recommend';
2222
import { NextPageContext } from 'next';
2323
import dynamic from 'next/dynamic';
2424
import React from 'react';
2525
import { getRequestHeaders } from '../../utils/utils';
26+
import { getBaseUrl } from '../../utils/get_base_url';
2627

2728
const TemplateCentre = dynamic(() => import('pc/components/template_centre/template_centre'), { ssr: true });
2829
const TemplatePreview = dynamic(() => import('pc/components/template_centre/template_preview'), { ssr: true });
@@ -38,8 +39,7 @@ const App = (props) => {
3839
};
3940

4041
export const getServerSideProps = async(context: NextPageContext) => {
41-
const host = process.env.API_PROXY;
42-
axios.defaults.baseURL = host + Url.BASE_URL;
42+
axios.defaults.baseURL = getBaseUrl(context);
4343
const headers = getRequestHeaders(context);
4444

4545
const userMeRes = await Api.getUserMe({}, false, headers);
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Url } from '@apitable/core';
2+
import { NextPageContext } from 'next';
3+
4+
export const getBaseUrl = (context: NextPageContext) => {
5+
const host = process.env.API_PROXY || `http://${context.req?.headers.host}` || '';
6+
return host + Url.BASE_URL;
7+
}

0 commit comments

Comments
 (0)