Base URL environment variable

This commit is contained in:
Dennis Reimann
2022-12-15 20:09:35 +01:00
parent 6be2db2161
commit 630b36f02d
2 changed files with 8 additions and 3 deletions

View File

@@ -2,7 +2,9 @@
const helpers = require('./helpers')
const IS_DEV = process.env.NODE_ENV === 'development'
const HOST = IS_DEV ? 'localhost:3000' : 'einundzwanzig.space'
const { DEPLOY_PRIME_URL, URL } = process.env
const BASE = DEPLOY_PRIME_URL || URL || 'https://einundzwanzig.space'
const HOST = IS_DEV ? 'http://localhost:3000' : BASE
const random = max => Math.floor(Math.random() * Math.floor(max))
const shuffle = arr => { for (let i = arr.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * i); const temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; }; return arr }
const formatDate = date => (new Date(date)).toISOString().replace(/T.*/, '').split('-').reverse().join('.')
@@ -15,7 +17,7 @@ const assetPath = path => {
}
const assetUrl = (path, protocol = 'https') => {
if (IS_DEV && !path.startsWith('http')) protocol = 'http'
const base = path.startsWith('http') ? '' : `${protocol}://${HOST}`
const base = path.startsWith('http') ? '' : HOST
let url = `${base}${assetPath(path)}`
if (!url.startsWith(`${protocol}:`)) url = url.replace(/^.*:/, `${protocol}:`)
return url