Skip to content

Commit

Permalink
use isDev check
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielCliftonGuardian committed Dec 19, 2024
1 parent bc62af9 commit 8d8cc7b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
10 changes: 5 additions & 5 deletions dotcom-rendering/src/lib/assets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ describe('decideAssetOrigin for stage', () => {
);
});
it('DEV', () => {
expect(decideAssetOrigin('DEV')).toEqual(BASE_URL_DEV);
expect(decideAssetOrigin('dev')).toEqual(BASE_URL_DEV);
expect(decideAssetOrigin(undefined)).toEqual(BASE_URL_DEV);
expect(decideAssetOrigin('DEV', true)).toEqual(BASE_URL_DEV);
expect(decideAssetOrigin('dev', true)).toEqual(BASE_URL_DEV);
expect(decideAssetOrigin(undefined, false)).toEqual('/');
});
});

Expand Down Expand Up @@ -82,13 +82,13 @@ describe('getPathFromManifest', () => {

it('returns correct hashed asset (1)', () => {
expect(getPathFromManifest('client.web', '7305.client.web.js')).toBe(
'http://localhost:3030/assets/7305.client.web.8cdc05567d98ebd9f67e.js',
'/assets/7305.client.web.8cdc05567d98ebd9f67e.js',
);
});

it('returns correct hashed asset (2)', () => {
expect(getPathFromManifest('client.web', '356.client.web.js')).toBe(
'http://localhost:3030/assets/356.client.web.0a1bbdf8c7a5e5826b7c.js',
'/assets/356.client.web.0a1bbdf8c7a5e5826b7c.js',
);
});

Expand Down
20 changes: 15 additions & 5 deletions dotcom-rendering/src/lib/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,38 @@ 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;
| 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/';
case 'CODE':
return 'https://assets-code.guim.co.uk/';
default:
return BASE_URL_DEV;
return '/';
}
};

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

export const ASSET_ORIGIN = decideAssetOrigin(process.env.GU_STAGE);
console.log(process.env.NODE_ENV);

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 8d8cc7b

Please sign in to comment.