diff --git a/gatsby/config-options/constants.ts b/gatsby/config-options/constants.ts index df6b6b053..78bd5952e 100644 --- a/gatsby/config-options/constants.ts +++ b/gatsby/config-options/constants.ts @@ -1,10 +1,11 @@ -import { buildGatsbyCloudPreviewUrl } from '../util/build-gatsby-cloud-preview-url'; +import { buildNetlifyPreviewUrl } from '../util/build-netlify-preview-url'; export const RSS_FEED_URL = '/blog/rss.xml'; export const DEFAULT_META_IMAGE_URL_PATH = '/sy-share-image.jpg'; -export const GATSBY_SITE_PREFIX = process.env.GATSBY_SITE_PREFIX || ''; -export const BRANCH_PREVIEW_URL = buildGatsbyCloudPreviewUrl({ - prefix: GATSBY_SITE_PREFIX, +export const NETLIFY_DOMAIN_NAME = process.env.GATSBY_NETLIFY_DOMAIN_NAME || ''; +export const BRANCH_PREVIEW_URL = buildNetlifyPreviewUrl({ + domainName: NETLIFY_DOMAIN_NAME, branch: process.env.BRANCH, + reviewId: process.env.REVIEW_ID, }); // either use a branch preview url if any diff --git a/gatsby/util/build-gatsby-cloud-preview-url.test.ts b/gatsby/util/build-gatsby-cloud-preview-url.test.ts deleted file mode 100644 index 05bf9414d..000000000 --- a/gatsby/util/build-gatsby-cloud-preview-url.test.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { buildGatsbyCloudPreviewUrl } from './build-gatsby-cloud-preview-url'; - -describe('build-gatsby-cloud-preview-url', () => { - test('should return a valid url', () => { - const url = buildGatsbyCloudPreviewUrl({ - prefix: 'satellytescommain', - branch: 'feature-branch-1', - }); - expect(url).toBe('https://satellytescommain-featurebranch1.gtsb.io'); - }); - - test('should return null for the production branch', () => { - const url = buildGatsbyCloudPreviewUrl({ - prefix: 'satellytescommain', - branch: 'main', - }); - expect(url).toBe(null); - }); - - test('should return null without a branch given', () => { - const url = buildGatsbyCloudPreviewUrl({ - prefix: 'satellytescommain', - branch: null, - }); - expect(url).toBe(null); - }); - - test('should return null if no prefix is given', () => { - const url = buildGatsbyCloudPreviewUrl({ - prefix: null, - branch: 'something', - }); - expect(url).toBe(null); - }); -}); diff --git a/gatsby/util/build-gatsby-cloud-preview-url.ts b/gatsby/util/build-gatsby-cloud-preview-url.ts deleted file mode 100644 index 1cce25385..000000000 --- a/gatsby/util/build-gatsby-cloud-preview-url.ts +++ /dev/null @@ -1,23 +0,0 @@ -/** - * Get the Gatsby Cloud Preview URL by branchName. If no branchName is given the - * fallback URL gets return. - * - * On gatsby cloud the deployment urls follow a strict pattern: - * - https://${DOMAIN_NAME}-${BRANCH_NAME}.gatsby.io - * - * Only lower case letters and numbers are used, everything else is filtered out. - * - */ - -const PRODUCTION_BRANCH = 'main'; -export const buildGatsbyCloudPreviewUrl = ({ prefix, branch }) => { - if (!prefix || !branch || branch === PRODUCTION_BRANCH) { - return null; - } - - const formattedBranchName = branch - .toLowerCase() - .replace(/[^a-zA-Z0-9]/gi, ''); - - return `https://${prefix}-${formattedBranchName}.gtsb.io`; -}; diff --git a/gatsby/util/build-netlify-preview-url.test.ts b/gatsby/util/build-netlify-preview-url.test.ts new file mode 100644 index 000000000..2aba8b28e --- /dev/null +++ b/gatsby/util/build-netlify-preview-url.test.ts @@ -0,0 +1,30 @@ +import { buildNetlifyPreviewUrl } from './build-netlify-preview-url'; + +describe('build-netlify-preview-url', () => { + test('should return a valid url', () => { + const url = buildNetlifyPreviewUrl({ + domainName: 'satellytes', + branch: 'feature-branch-1', + reviewId: '123', + }); + expect(url).toBe('https://deploy-preview-123--satellytes.netlify.app/'); + }); + + test('should return null for the production branch', () => { + const url = buildNetlifyPreviewUrl({ + domainName: 'satellytescommain', + branch: 'main', + reviewId: '123', + }); + expect(url).toBe(null); + }); + + test('should return null if no domainName is given', () => { + const url = buildNetlifyPreviewUrl({ + domainName: null, + branch: 'something', + reviewId: '123', + }); + expect(url).toBe(null); + }); +}); diff --git a/gatsby/util/build-netlify-preview-url.ts b/gatsby/util/build-netlify-preview-url.ts new file mode 100644 index 000000000..84442ebca --- /dev/null +++ b/gatsby/util/build-netlify-preview-url.ts @@ -0,0 +1,8 @@ +const PRODUCTION_BRANCH = 'main'; +export const buildNetlifyPreviewUrl = ({ domainName, reviewId, branch }) => { + if (!domainName || !reviewId || branch === PRODUCTION_BRANCH) { + return null; + } + + return `https://deploy-preview-${reviewId}--${domainName}.netlify.app/`; +};