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

Add subpath support #105

Closed
wants to merge 4 commits into from
Closed
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
2 changes: 1 addition & 1 deletion backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module github.com/HuolalaTech/page-spy-web/backend

go 1.21

require github.com/HuolalaTech/page-spy-api v1.3.1
require github.com/HuolalaTech/page-spy-api v1.3.2-alpha.1

require (
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
Expand Down
2 changes: 2 additions & 0 deletions backend/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/HuolalaTech/page-spy-api v1.3.1 h1:DdoOgsFs/SwjIrcrc6N+cXDsFN213wVPixDzk4WKfX8=
github.com/HuolalaTech/page-spy-api v1.3.1/go.mod h1:njVR6tXPbDvvA/ecpLY5+9Pu43bOI5tnK+yscNdiMkU=
github.com/HuolalaTech/page-spy-api v1.3.2-alpha.1 h1:giDGOOrqwKuT794PJGM+QUwWH2zSesQebOSsYTLQIXE=
github.com/HuolalaTech/page-spy-api v1.3.2-alpha.1/go.mod h1:njVR6tXPbDvvA/ecpLY5+9Pu43bOI5tnK+yscNdiMkU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
56 changes: 30 additions & 26 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>PageSpy</title>
<link rel="icon" href="/favicon.ico" />
<link rel="shortcut icon" type="image/svg+xml" href="/favicon.svg" />
<script src="source-map/source-map.min.js"></script>
<script>
const mappingWasmUrl = new URL(
window.location.pathname + '/source-map/mappings.wasm',
window.location.href,
).toString();
sourceMap.SourceMapConsumer.initialize({
'lib/mappings.wasm': mappingWasmUrl,
});
</script>
<script src="shiki/dist/index.jsdelivr.iife.js"></script>
<script>
const shikiURL = new URL(
window.location.pathname + '/shiki',
window.location.origin,
).toString();
window.shiki.setCDN(shikiURL);
</script>
</head>

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PageSpy</title>
<link rel="icon" href="/favicon.ico">
<link rel="shortcut icon" type="image/svg+xml" href="/favicon.svg">
<script src="/source-map/source-map.min.js"></script>
<script>
const mappingWasmUrl = new URL('/source-map/mappings.wasm', window.location.href).toString()
sourceMap.SourceMapConsumer.initialize({
"lib/mappings.wasm": mappingWasmUrl
});
</script>
<script src="/shiki/dist/index.jsdelivr.iife.js"></script>
<script>
const shikiURL = new URL('/shiki', window.location.origin).toString()
window.shiki.setCDN(shikiURL)
</script>
</head>

<body>
<div id="root"></div>
<script src="/src/main.tsx" type="module"></script>
</body>

<body>
<div id="root"></div>
<script src="src/main.tsx" type="module"></script>
</body>
</html>
6 changes: 3 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import { isDoc } from './utils/constants';
import { ErrorBoundary } from './components/ErrorBoundary';

// const basename = isDoc ? '/page-spy-web' : '/';
const basename = '/';
const basename = window.location.pathname;

export const App = () => {
const [lang] = useLanguage();

return (
<React.StrictMode>
<BrowserRouter basename={basename}>
<HashRouter>
<ErrorBoundary>
<ConfigProvider
locale={lang === 'zh' ? zh : en}
Expand All @@ -30,7 +30,7 @@ export const App = () => {
<RouteConfig />
</ConfigProvider>
</ErrorBoundary>
</BrowserRouter>
</HashRouter>
</React.StrictMode>
);
};
2 changes: 1 addition & 1 deletion src/apis/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const removeUndefinedValues = (params: unknown) => {
};

class ApiRequest {
defaultPrefix = resolveProtocol()[0] + API_BASE_URL + '/api/v1';
defaultPrefix = 'api/v1';

get<T>(path: string, options?: Options) {
return this.request<T>('GET', path, options);
Expand Down
39 changes: 38 additions & 1 deletion vite.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,30 @@ import path from 'path';
import react from '@vitejs/plugin-react';
import svgr from 'vite-plugin-svgr';
import mdx from '@mdx-js/rollup';
import fs from 'fs';
import { promisify } from 'util';
import globby from 'globby';

const writeFile = promisify(fs.writeFile);
const readFile = promisify(fs.readFile);

export default ({ mode, command }) => {
const buildDoc = mode === 'doc';
const isProd = command === 'build';

const baseURL = (() => {
if (buildDoc) {
return './';
}
if (!isProd) {
return '/';
}

return '__BASE_PLACEHOLDER__';
})();

return defineConfig({
base: buildDoc ? './' : '/',
base: baseURL,
build: {
target: ['chrome100'],
sourcemap: isProd ? 'hidden' : true,
Expand All @@ -35,6 +52,26 @@ export default ({ mode, command }) => {
},
react(),
svgr(),
{
async closeBundle() {
if (buildDoc) {
return;
}
const paths = await globby('dist', {
expandDirectories: {
extensions: ['html', 'css', 'js'],
},
});
const tasks = paths.map(async (filepath) => {
const buffer = await readFile(filepath);
const content = buffer
.toString()
.replace(/\/__BASE_PLACEHOLDER__\/?/g, '');
await writeFile(filepath, content);
});
await Promise.all(tasks);
},
},
],
});
};