Skip to content

Commit

Permalink
Fix asset url path for development (#13038)
Browse files Browse the repository at this point in the history
* fix/asset-url-path
  • Loading branch information
DanielCliftonGuardian authored Dec 20, 2024
1 parent e3623c4 commit c46b897
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
7 changes: 4 additions & 3 deletions dotcom-rendering/src/lib/assets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { readFileSync } from 'node:fs';
import { BUILD_VARIANT } from '../../webpack/bundles';
import {
APPS_SCRIPT,
BASE_URL_DEV,
decideAssetOrigin,
getModulesBuild,
getPathFromManifest,
Expand All @@ -27,9 +28,9 @@ describe('decideAssetOrigin for stage', () => {
);
});
it('DEV', () => {
expect(decideAssetOrigin('DEV')).toEqual('/');
expect(decideAssetOrigin('dev')).toEqual('/');
expect(decideAssetOrigin(undefined)).toEqual('/');
expect(decideAssetOrigin('DEV', true)).toEqual(BASE_URL_DEV);
expect(decideAssetOrigin('dev', true)).toEqual(BASE_URL_DEV);
expect(decideAssetOrigin(undefined, false)).toEqual('/');
});
});

Expand Down
16 changes: 13 additions & 3 deletions dotcom-rendering/src/lib/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,28 @@ interface AssetHash {
[key: string]: string;
}

export const BASE_URL_DEV = 'http://localhost:3030/';

export type AssetOrigin =
| 'https://assets.guim.co.uk/'
| 'https://assets-code.guim.co.uk/'
| typeof BASE_URL_DEV
| '/';

/**
* Decides the url to use for fetching assets
*
* @param {'PROD' | 'CODE' | undefined} stage the environment code is executing in
* @returns {string}
* @param {boolean} isDev whether the environment is development
* @returns {AssetOrigin}
*/
export const decideAssetOrigin = (stage: string | undefined): AssetOrigin => {
export const decideAssetOrigin = (
stage: string | undefined,
isDev?: boolean,
): AssetOrigin => {
if (isDev) {
return BASE_URL_DEV;
}
switch (stage?.toUpperCase()) {
case 'PROD':
return 'https://assets.guim.co.uk/';
Expand All @@ -33,7 +43,7 @@ export const decideAssetOrigin = (stage: string | undefined): AssetOrigin => {

const isDev = process.env.NODE_ENV === 'development';

export const ASSET_ORIGIN = decideAssetOrigin(process.env.GU_STAGE);
export const ASSET_ORIGIN = decideAssetOrigin(process.env.GU_STAGE, isDev);

const isAssetHash = (manifest: unknown): manifest is AssetHash =>
isObject(manifest) &&
Expand Down

0 comments on commit c46b897

Please sign in to comment.