Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix asset url path for development #13038

Merged
merged 7 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
DanielCliftonGuardian marked this conversation as resolved.
Show resolved Hide resolved
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
Loading