Skip to content

Commit 457b0ff

Browse files
committed
Add support for subpath deployment
Fix #31
1 parent fd5ae65 commit 457b0ff

File tree

5 files changed

+37
-6
lines changed

5 files changed

+37
-6
lines changed

packages/client/.env

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@ REACT_APP_STAC_API=
88

99
## Theming
1010
# REACT_APP_THEME_PRIMARY_COLOR='#6A5ACD'
11-
# REACT_APP_THEME_SECONDARY_COLOR='#048A81'
11+
# REACT_APP_THEME_SECONDARY_COLOR='#048A81'
12+
13+
## Don't set the public url here. Check the README.md file for more information
14+
# PUBLIC_URL= Do not set here

packages/client/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Some client options are controlled by environment variables. These are:
1414
## Title and description of the app for metadata
1515
APP_TITLE
1616
APP_DESCRIPTION
17+
PUBLIC_URL
1718
1819
# API
1920
## If the app is being served in from a subfolder, the domain url must be set.
@@ -26,6 +27,15 @@ REACT_APP_THEME_PRIMARY_COLOR
2627
REACT_APP_THEME_SECONDARY_COLOR
2728
```
2829

30+
**Public URL**
31+
It is recommended to always set the `PUBLIC_URL` environment variable on a production build.
32+
If the app is being served from a subfolder, the `PUBLIC_URL` should include the subfolder path. **Do not include a trailing slash.**
33+
34+
For example, if the app is being served from `https://example.com/stac-manager`, the `PUBLIC_URL` should be set to `https://example.com/stac-manager`.
35+
36+
> [!IMPORTANT]
37+
> The `PUBLIC_URL` environment variable must be set before running the build script, and therefore the `.env` file cannot be used to set this variable.
38+
2939
You must provide a value for the `REACT_APP_STAC_API` environment variable. This should be the URL of the STAC API you wish to interact with.
3040

3141
If the `REACT_APP_STAC_BROWSER` environment variable is not set, [Radiant Earth's STAC Browser](https://radiantearth.github.io/stac-browser/) will be used by default, which will connect to the STAC API specified in `REACT_APP_STAC_API`.

packages/client/src/App.tsx

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,21 @@ import CollectionDetail from './pages/CollectionDetail';
2525
import Sandbox from './pages/Sandbox';
2626
import { config } from './plugin-system/config';
2727

28+
let basename: string | undefined;
29+
if (process.env.PUBLIC_URL) {
30+
try {
31+
basename = new URL(process.env.PUBLIC_URL).pathname;
32+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
33+
} catch (error) {
34+
// no-op
35+
}
36+
}
37+
2838
export const App = () => (
2939
<ChakraProvider theme={theme}>
3040
<StacApiProvider apiUrl={process.env.REACT_APP_STAC_API!}>
3141
<PluginConfigProvider config={config}>
32-
<Router>
42+
<Router basename={basename}>
3343
<Container
3444
maxW='container.xl'
3545
minH='100vh'
@@ -46,7 +56,7 @@ export const App = () => (
4656
>
4757
<Flex gap={4} alignItems='center'>
4858
<Image
49-
src='/meta/icon-512.png'
59+
src={`${process.env.PUBLIC_URL}/meta/icon-512.png`}
5060
width={8}
5161
aspectRatio={1}
5262
borderRadius='md'

packages/client/src/pages/Home.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import React from 'react';
2+
import { Navigate } from 'react-router-dom';
23

34
import { usePageTitle } from '../hooks';
4-
import { Navigate } from 'react-router-dom';
55

66
function Home() {
7-
usePageTitle('STAC Admin');
7+
usePageTitle(process.env.APP_TITLE!);
88

99
return <Navigate to='/collections' replace />;
1010
}

packages/client/tasks/build.mjs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,22 @@ async function copyFiles() {
3535
}
3636

3737
async function parcelServe() {
38+
const publicUrl = process.env.PUBLIC_URL || '';
39+
40+
if (publicUrl) {
41+
log.warn(`🌍 Building using public URL: ${publicUrl}`);
42+
} else {
43+
log.warn(`🌍 Building without public URL`);
44+
}
45+
3846
const bundler = new Parcel({
3947
entries: `${__dirname}/../src/index.html`,
4048
defaultConfig: `${__dirname}/../.parcelrc`,
4149
cacheDir: `${__dirname}/../.parcel-cache`,
4250
mode: 'production',
4351
defaultTargetOptions: {
4452
distDir: `${__dirname}/../dist`,
45-
publicUrl: process.env.PUBLIC_URL || '/'
53+
publicUrl
4654
},
4755
additionalReporters: [
4856
{

0 commit comments

Comments
 (0)