diff options
| -rw-r--r-- | CHANGELOG.md | 4 | ||||
| -rw-r--r-- | eslint.config.js | 14 | ||||
| -rw-r--r-- | netlify-pr.go | 66 | ||||
| -rw-r--r-- | netlify-preview.js | 58 | ||||
| -rw-r--r-- | netlify.toml | 2 | ||||
| -rw-r--r-- | package.json | 3 |
6 files changed, 79 insertions, 68 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index d955eb8..fb46543 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 script for `server` in `package.json`. This change addresses a deprecation warning introduced in Hugo v0.114.0, ensuring compatibility with future versions of Hugo. +- Replace Go script with Node.js implementation for Netlify redirects patching + for Deploy Preview context. This change aims to eliminate the dependency on Go + for the theme's development environment, and streamline the project's technology + stack. ### Fixed diff --git a/eslint.config.js b/eslint.config.js index 054bd46..93ee895 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -112,5 +112,19 @@ module.exports = [ ...globals.node } } + }, + { + files: ['netlify-preview.js'], + rules: { + // allow use of console + 'no-console': 'off' + }, + languageOptions: { + 'sourceType': 'commonjs', + globals: { + // Browser global variables + ...globals.node + } + } } ]; diff --git a/netlify-pr.go b/netlify-pr.go deleted file mode 100644 index 58fac5d..0000000 --- a/netlify-pr.go +++ /dev/null @@ -1,66 +0,0 @@ -package main - -// Path netlify.toml file for Netlify Deploy Preview to allow some -// violation for CSP header. - -import ( - "fmt" - "log" - "os" - "regexp" - "strings" -) - -const netlifyConfig = "netlify.toml" - -func main() { - input, err := os.ReadFile(netlifyConfig) - if err != nil { - log.Fatalln(err) - } - - lines := strings.Split(string(input), "\n") - for i, line := range lines { - newStr := line - - // -> default-src 'self'; - // <- default-src 'self' blob:; - reStr := regexp.MustCompile("(default-src) ('self')(;)") - repStr := "${1} ${2} blob:${3}" - newStr = reStr.ReplaceAllString(newStr, repStr) - - // -> style-src 'self' cdn.hypothes.is; - // <- style-src 'self' 'unsafe-inline' cdn.hypothes.is; - reStr = regexp.MustCompile(`(style-src) ('self') (cdn\.hypothes\.is)(;)`) - repStr = "${1} ${2} ${3} 'unsafe-inline'${4}" - newStr = reStr.ReplaceAllString(newStr, repStr) - - // -> media-src 'self'; - // <- media-src 'self' blob: https://app.netlify.com; - reStr = regexp.MustCompile("(media-src) ('self')(;)") - repStr = "${1} ${2} blob: https://app.netlify.com${3}" - newStr = reStr.ReplaceAllString(newStr, repStr) - - // -> frame-src hypothes.is; - // <- frame-src hypothes.is app.netlify.com; - reStr = regexp.MustCompile(`(frame-src) (hypothes\.is)(;)`) - repStr = "${1} ${2} app.netlify.com${3}" - newStr = reStr.ReplaceAllString(newStr, repStr) - - // -> script-src 'self' www.googletagmanager.com hypothes.is cdn.hypothes.is; - // <- script-src 'self' www.googletagmanager.com hypothes.is cdn.hypothes.is netlify-cdp-loader.netlify.app; - reStr = regexp.MustCompile(`(script-src) ('self' www\.googletagmanager\.com hypothes\.is cdn\.hypothes\.is)(;)`) - repStr = "${1} ${2} netlify-cdp-loader.netlify.app${3}" - newStr = reStr.ReplaceAllString(newStr, repStr) - - lines[i] = newStr - } - - output := strings.Join(lines, "\n") - err = os.WriteFile(netlifyConfig, []byte(output), 0644) - if err != nil { - log.Fatalln(err) - } - - fmt.Println("Done") -} diff --git a/netlify-preview.js b/netlify-preview.js new file mode 100644 index 0000000..5b37433 --- /dev/null +++ b/netlify-preview.js @@ -0,0 +1,58 @@ +'use strict'; + +const fs = require('fs'); +const path = require('path'); + +const netlifyConfig = 'netlify.toml'; + +// Read the netlify.toml file +fs.readFile(path.resolve(__dirname, netlifyConfig), 'utf8', (err, data) => { + if (err) { + console.error('Error reading file: ', err); + return; + } + + // Split the file content into lines + const lines = data.split('\n'); + + // Process each line + const processedLines = lines.map((line) => { + // -> default-src 'self'; + // <- default-src 'self' blob:; + line = line.replace(/(default-src) ('self')(;)/, '$1 $2 blob:$3'); + + // -> style-src 'self' cdn.hypothes.is; + // <- style-src 'self' 'unsafe-inline' cdn.hypothes.is; + line = line.replace(/(style-src) ('self') (cdn\.hypothes\.is)(;)/, '$1 $2 $3 \'unsafe-inline\'$4'); + + // -> media-src 'self'; + // <- media-src 'self' blob: https://app.netlify.com; + line = line.replace(/(media-src) ('self')(;)/, '$1 $2 blob: https://app.netlify.com$3'); + + // -> frame-src hypothes.is; + // <- frame-src hypothes.is app.netlify.com; + line = line.replace(/(frame-src) (hypothes\.is)(;)/, '$1 $2 app.netlify.com$3'); + + // -> script-src 'self' www.googletagmanager.com hypothes.is cdn.hypothes.is; + // <- script-src 'self' www.googletagmanager.com hypothes.is cdn.hypothes.is netlify-cdp-loader.netlify.app; + line = line.replace( + /(script-src) ('self' www\.googletagmanager\.com hypothes\.is cdn\.hypothes\.is)(;)/, + '$1 $2 netlify-cdp-loader.netlify.app$3' + ); + + return line; + }); + + // Join the processed lines back into a single string + const output = processedLines.join('\n'); + + // Write the modified content back to the file + fs.writeFile(path.resolve(__dirname, netlifyConfig), output, 'utf8', (err) => { + if (err) { + console.error('Error writing file:', err); + return; + } + + console.log('Done'); + }); +}); diff --git a/netlify.toml b/netlify.toml index c14d827..55134eb 100644 --- a/netlify.toml +++ b/netlify.toml @@ -27,7 +27,7 @@ # Deploy Preview context: all deploys generated from # a pull/merge request will inherit these settings. [context.deploy-preview] - command = 'go run netlify-pr.go; hugo --source=exampleSite --buildDrafts --buildFuture --baseURL ${DEPLOY_PRIME_URL} --destination ../public --minify' + command = 'npm run netlify-preview; hugo --source=exampleSite --buildDrafts --buildFuture --baseURL ${DEPLOY_PRIME_URL} --destination ../public --minify' [context.deploy-preview.environment] HUGO_ENV = 'development' diff --git a/package.json b/package.json index c177bf8..f79ee7c 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,8 @@ "test": "playwright test", "lint": "npm run lint:js && npm run lint:editorconfig", "lint:editorconfig": "editorconfig-checker", - "lint:js": "eslint static/js/* assets/js/* tests/*.spec.js eslint.config.js playwright.config.js" + "lint:js": "eslint static/js/* assets/js/* tests/*.spec.js eslint.config.js playwright.config.js netlify-preview.js", + "netlify-preview": "node netlify-preview.js" }, "devDependencies": { "@playwright/test": "^1.43.0", |
