Skip to content

Commit 04dd141

Browse files
author
Thomas van de Put
authored
Merge pull request #86 from holidayextras/redirects
Add ability to write redirects for Paultons
2 parents 5cadf93 + 69ee625 commit 04dd141

File tree

3 files changed

+30
-1
lines changed

3 files changed

+30
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@holidayextras/static-site-generator",
3-
"version": "9.1.1",
3+
"version": "9.2.0",
44
"description": "Holiday Extras Static Site Generator in metalsmith / react",
55
"repository": {
66
"type": "git",

src/getDataSource.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import prismic from './metalsmith-prismic'
22
import hxseo from './getHXSEOContent'
3+
import writeRedirect from './writeRedirect'
34
import apiCaller from './apiCaller'
45
import _ from 'lodash'
56

@@ -16,12 +17,18 @@ const getDataSource = (opts) => {
1617
accessToken: opts.dataSource.accessToken,
1718
linkResolver: configLinkResolver || function (ctx, doc) {
1819
if (doc.isBroken) return ''
20+
21+
// create redirect script if needed
22+
writeRedirect(doc.data)
23+
24+
// Page url
1925
if (_.has(doc, 'data.slug.json.value')) {
2026
const regExpDomain = new RegExp(`.*${opts.config.domainSettings.domainLive}`)
2127
// Strip domain (+ everything before it) off of the slug in case it was added by mistake
2228
const slug = doc.data.slug.json.value
2329
return slug.replace(regExpDomain, '')
2430
}
31+
2532
return '/' + doc.uid
2633
}
2734
})

src/writeRedirect.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import fs from 'fs'
2+
3+
const writeRedirect = (documentData) => {
4+
// Prismic custom field with redirect value
5+
const redirectValue = documentData?.redirect?.json?.value
6+
// Prismic custom field for slug or UID which every document has by default
7+
const pageUrl = documentData?.slug?.json?.value || documentData.uid
8+
if (!redirectValue || !pageUrl || !redirectValue.includes('https://')) return
9+
10+
try {
11+
const redirectCommand = `
12+
aws s3 cp s3://$BUCKET/${pageUrl}.html s3://$BUCKET/${pageUrl}.html --website-redirect ${redirectValue}
13+
aws s3 cp s3://$BUCKET/${pageUrl} s3://$BUCKET/${pageUrl} --website-redirect ${redirectValue}`
14+
if (!fs.existsSync('./bin')) fs.mkdirSync('./bin')
15+
fs.appendFileSync('./bin/redirects.sh', redirectCommand, { mode: 0o755 })
16+
console.log(`Added redirect for ${pageUrl} to ${redirectValue}`)
17+
} catch (err) {
18+
console.log(`Writing redirect to bash file for ${pageUrl} has failed.`, err)
19+
}
20+
}
21+
22+
export default writeRedirect

0 commit comments

Comments
 (0)