Skip to content

Commit

Permalink
feat: BASE_URL can be overridden (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtPooki authored Feb 22, 2024
1 parent c35af58 commit 2ba597a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
5 changes: 1 addition & 4 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,10 @@ import './app.css'
import App from './app.tsx'
import { loadConfigFromLocalStorage } from './lib/config-db.ts'
import { isPathOrSubdomainRequest } from './lib/path-or-subdomain.ts'
import { BASE_URL } from './lib/webpack-constants.ts'
import RedirectPage from './redirectPage.tsx'

await loadConfigFromLocalStorage()
/**
* You can change the BASE_URL when deploying this app to a different domain.
*/
const BASE_URL = process.env.BASE_URL ?? 'helia-sw-gateway.localhost'

const container = document.getElementById('root')

Expand Down
10 changes: 10 additions & 0 deletions src/lib/webpack-constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* eslint-disable @typescript-eslint/no-non-null-assertion */

/**
* You can change the BASE_URL when deploying this app to a different domain.
*/
const BASE_URL = process.env.BASE_URL!

export {
BASE_URL
}
2 changes: 1 addition & 1 deletion src/sw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import mime from 'mime-types'
import { getHelia } from './get-helia.ts'
import { heliaFetch } from './lib/heliaFetch.ts'
import { BASE_URL } from './lib/webpack-constants.ts'
import type { Helia } from '@helia/interface'

declare let self: ServiceWorkerGlobalScope
Expand Down Expand Up @@ -91,7 +92,6 @@ const isRootRequestForContent = (event: FetchEvent): boolean => {
}

function getSubdomainParts (request: Request): { origin: string | null, protocol: string | null } {
const BASE_URL = 'helia-sw-gateway.localhost'
const urlString = request.url
const url = new URL(urlString)
const subdomain = url.hostname.replace(`.${BASE_URL}`, '')
Expand Down
6 changes: 4 additions & 2 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,7 @@ const dev = {
// Only update what has changed on hot reload
hot: true,
port: 3000,
allowedHosts: ['helia-sw.dev.local']

allowedHosts: [process.env.BASE_URL ?? 'helia-sw-gateway.localhost', 'localhost']
},

plugins: [
Expand Down Expand Up @@ -170,6 +169,9 @@ const common = {
new webpack.DefinePlugin({
window: 'globalThis' // attempt to naively replace all "window" keywords with "globalThis"
}),
new webpack.EnvironmentPlugin({
BASE_URL: process.env.BASE_URL ?? 'helia-sw-gateway.localhost'
}),
new CompressionPlugin({
algorithm: 'gzip',
test: /\.(js|css|html|svg)$/,
Expand Down

0 comments on commit 2ba597a

Please sign in to comment.