Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: web-server local start axios base url #155

Merged
merged 1 commit into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}