Skip to content

Commit

Permalink
feat(project): hash based router for github pages
Browse files Browse the repository at this point in the history
  • Loading branch information
jwpradm committed Dec 16, 2021
1 parent 3249d63 commit 2250f33
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions scripts/deploy-github.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ function getEnv() {
return {
...process.env,
SNOWPACK_PUBLIC_BASE_URL: getBaseUrl(),
SNOWPACK_PUBLIC_GITHUB_PAGES: true,
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React, { Component } from 'react';
import { BrowserRouter as Router } from 'react-router-dom';
import { I18nextProvider, getI18n } from 'react-i18next';
import type { Config } from 'types/Config';

import Router from './components/Router/Router';
import Root from './components/Root/Root';
import ConfigProvider from './providers/ConfigProvider';
import QueryProvider from './providers/QueryProvider';
Expand All @@ -11,7 +11,6 @@ import './styles/main.scss';
import { restoreWatchHistory } from './stores/WatchHistoryStore';
import { initializeFavorites } from './stores/FavoritesStore';
import { initializeAccount } from './stores/AccountStore';
import { getPublicUrl } from './utils/domHelpers';

interface State {
error: Error | null;
Expand Down Expand Up @@ -61,7 +60,7 @@ class App extends Component {
onValidationError={this.configErrorHandler}
onValidationCompleted={this.configValidationCompletedHandler}
>
<Router basename={getPublicUrl('/')}>
<Router>
<Root error={this.state.error} />
</Router>
</ConfigProvider>
Expand Down
10 changes: 10 additions & 0 deletions src/components/Router/Router.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react';
import { BrowserRouter, HashRouter } from 'react-router-dom';
import type { BrowserRouterProps, HashRouterProps } from 'react-router-dom';

export default function Router(props: BrowserRouterProps | HashRouterProps) {
if (import.meta.env.SNOWPACK_PUBLIC_GITHUB_PAGES) {
return <HashRouter {...props} />;
}
return <BrowserRouter {...props} />;
}

0 comments on commit 2250f33

Please sign in to comment.