Skip to content

Commit

Permalink
fix: web-server local start axios base url (#155)
Browse files Browse the repository at this point in the history
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?
-->
  • Loading branch information
wangkailang committed Jan 6, 2023
1 parent 16688f3 commit a081f48
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 13 deletions.
6 changes: 2 additions & 4 deletions packages/datasheet/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,20 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { Url } from '@apitable/core';
import axios from 'axios';
import { NextPageContext } from 'next';
import { getBaseUrl } from '../utils/get_base_url';

const App = () => {
return null;
};

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

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

const cookie = context.req?.headers.cookie;
const headers: Record<string, string> = {};

Expand Down
6 changes: 3 additions & 3 deletions packages/datasheet/pages/share/[...all].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,13 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { Api, Url } from '@apitable/core';
import { Api } from '@apitable/core';
import axios from 'axios';
import { NextPageContext } from 'next';
import dynamic from 'next/dynamic';
import { getRegResult, shareIdReg } from 'pc/hooks';
import React from 'react';
import { getBaseUrl } from '../../utils/get_base_url';

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

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

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

if (!context.req?.url) {
return { props: {}};
Expand Down
6 changes: 3 additions & 3 deletions packages/datasheet/pages/template/[category_code]/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { Api, ConfigConstant, Url } from '@apitable/core';
import { Api, ConfigConstant } from '@apitable/core';
import axios from 'axios';
import { TemplateListContext } from 'context/template_list';
import { TemplateRecommendContext } from 'context/template_recommend';
import { NextPageContext } from 'next';
import dynamic from 'next/dynamic';
import React from 'react';
import { getRequestHeaders } from '../../../utils/utils';
import { getBaseUrl } from '../../../utils/get_base_url';

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

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

if (categoryCode === 'album') {
return { props: {}};
Expand All @@ -57,7 +57,7 @@ export const getServerSideProps = async(context: NextPageContext) => {
return { props: {}};
}

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

const userMeRes = await Api.getUserMe({}, false, headers);
Expand Down
6 changes: 3 additions & 3 deletions packages/datasheet/pages/template/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { Api, Url } from '@apitable/core';
import { Api } from '@apitable/core';
import axios from 'axios';
import { TemplateRecommendContext } from 'context/template_recommend';
import { NextPageContext } from 'next';
import dynamic from 'next/dynamic';
import React from 'react';
import { getRequestHeaders } from '../../utils/utils';
import { getBaseUrl } from '../../utils/get_base_url';

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

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

const userMeRes = await Api.getUserMe({}, false, headers);
Expand Down
7 changes: 7 additions & 0 deletions packages/datasheet/utils/get_base_url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Url } from '@apitable/core';
import { NextPageContext } from 'next';

export const getBaseUrl = (context: NextPageContext) => {
const host = process.env.API_PROXY || `http://${context.req?.headers.host}` || '';
return host + Url.BASE_URL;
}

0 comments on commit a081f48

Please sign in to comment.