Skip to content

Commit 55d7013

Browse files
committed
refactor: Use variable DEPLOY_URL for Netlify PR previews
1 parent 51cbfc8 commit 55d7013

5 files changed

Lines changed: 23 additions & 15 deletions

File tree

index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
<meta name="viewport" content="width=device-width, initial-scale=1.0, minimal-ui">
88
<meta name="color-scheme" content="dark light">
99
<meta name="theme-color" content="#673AB8">
10-
<link rel="alternate" type="application/rss+xml" href="https://preactjs.com/feed.xml">
11-
<link rel="alternate" type="application/atom+xml" href="https://preactjs.com/feed.atom">
10+
<link rel="alternate" type="application/rss+xml" href="%NETLIFY_DEPLOY_URL%/feed.xml">
11+
<link rel="alternate" type="application/atom+xml" href="%NETLIFY_DEPLOY_URL%/feed.atom">
1212
<meta property="og:type" content="website">
13-
<meta property="og:image" content="https://preactjs.com/og-image.png">
13+
<meta property="og:image" content="%NETLIFY_DEPLOY_URL%/og-image.png">
1414
<meta name="twitter:card" content="summary_large_image">
1515
<meta name="twitter:site" content="@preactjs">
1616
<link href="https://cdn.jsdelivr.net" rel="preconnect" crossorigin="anonymous">

plugins/netlify.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import path from 'node:path';
2-
import { pathToFileURL } from 'node:url'
2+
import { pathToFileURL } from 'node:url';
33
import { Readable } from 'stream';
44

55
/**

plugins/rss-feed.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,31 @@ import config from '../src/config.json';
44
/**
55
* @returns {import('vite').Plugin}
66
*/
7-
export function rssFeedPlugin() {
7+
export function rssFeedPlugin(deployURL) {
88
return {
99
name: 'rss-feed',
1010
apply: 'build',
1111
generateBundle() {
1212
const feed = new Feed({
1313
title: 'Preact Blog',
1414
description: 'Preact news and articles',
15-
id: 'https://preactjs.com',
16-
link: 'https://preactjs.com',
15+
id: deployURL,
16+
link: deployURL,
1717
language: 'en',
18-
image: 'https://preactjs.com/assets/branding/symbol.png',
19-
favicon: 'https://preactjs.com/favicon.ico',
18+
image: `${deployURL}/assets/branding/symbol.png`,
19+
favicon: `${deployURL}/favicon.ico`,
2020
copyright: 'All rights reserved 2022, the Preact team',
2121
feedLinks: {
22-
json: 'https://preactjs.com/json',
23-
atom: 'https://preactjs.com/atom'
22+
json: `${deployURL}/json`,
23+
atom: `${deployURL}/atom`
2424
}
2525
});
2626

2727
config.blog.forEach(post => {
2828
feed.addItem({
2929
title: post.name.en,
30-
id: `https://preactjs.com${post.path}`,
31-
link: `https://preactjs.com${post.path}`,
30+
id: `${deployURL}${post.path}`,
31+
link: `${deployURL}${post.path}`,
3232
description: post.excerpt.en,
3333
date: new Date(post.date)
3434
});

src/index.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export async function prerender() {
4646

4747
const elements = new Set([
4848
{ type: 'meta', props: { name: 'description', content: globalThis.description } },
49-
{ type: 'meta', props: { property: 'og:url', content: `https://preactjs.com${location.pathname}` } },
49+
{ type: 'meta', props: { property: 'og:url', content: `${import.meta.env.NETLIFY_DEPLOY_URL}${location.pathname}` } },
5050
{ type: 'meta', props: { property: 'og:title', content: globalThis.title } },
5151
{ type: 'meta', props: { property: 'og:description', content: globalThis.description } },
5252
location.pathname.includes('/v8/') && { type: 'meta', props: { name: 'robots', content: 'noindex' } },

vite.config.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import { spaFallbackMiddlewarePlugin } from './plugins/spa-fallback-middleware.j
1010
import { htmlRoutingMiddlewarePlugin } from './plugins/html-routing-middleware.js';
1111
import { rssFeedPlugin } from './plugins/rss-feed.js';
1212

13+
const NETLIFY_DEPLOY_URL = process.env.CONTEXT === 'deploy-preview'
14+
? process.env.DEPLOY_PRIME_URL
15+
: 'https://preactjs.com';
16+
1317
export default defineConfig({
1418
publicDir: 'src/assets',
1519
optimizeDeps: {
@@ -19,7 +23,11 @@ export default defineConfig({
1923
target: ['chrome88', 'edge88', 'es2020', 'firefox78', 'safari14'],
2024
outDir: 'build'
2125
},
26+
define: {
27+
'import.meta.env.NETLIFY_DEPLOY_URL': JSON.stringify(NETLIFY_DEPLOY_URL)
28+
},
2229
plugins: [
30+
// @ts-ignore
2331
replace({
2432
'process.env.BRANCH': JSON.stringify(process.env.BRANCH),
2533
preventAssignment: true
@@ -53,6 +61,6 @@ export default defineConfig({
5361
netlifyPlugin(),
5462
spaFallbackMiddlewarePlugin(),
5563
htmlRoutingMiddlewarePlugin(),
56-
rssFeedPlugin()
64+
rssFeedPlugin(NETLIFY_DEPLOY_URL)
5765
]
5866
});

0 commit comments

Comments
 (0)