Last active
June 6, 2020 11:13
-
-
Save takumifukasawa/a035112fdd16891608a4677b5c2f1201 to your computer and use it in GitHub Desktop.
【GatsbyJS】Basic認証下でiOS safariからページを開くとリロードする度に認証を求められる場合の対処法 https://takumifukasawa.hatenablog.com/entry/gatsby-ios-safari-basic-auth
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const path = require("path") | |
const glob = require("glob") | |
const { map } = require("lodash") | |
// specify html files by magic path | |
const htmlFilePath = "public/**/*.html" | |
function readFile(path, encode = "utf-8") { | |
return new Promise((resolve, reject) => { | |
fs.readFile(path, encode, (err, data) => { | |
if (err) reject() | |
resolve(data) | |
}) | |
}) | |
} | |
function writeFile(fileName, data) { | |
return new Promise((resolve, reject) => { | |
fs.writeFile(fileName, data, err => { | |
if (err) reject() | |
resolve() | |
}) | |
}) | |
} | |
async function getFileList(pattern) { | |
return new Promise((resolve, reject) => { | |
glob(pattern, (err, res) => { | |
if (err) reject(err) | |
resolve(res) | |
}) | |
}) | |
} | |
async function replaceTag(fileName) { | |
const content = await readFile(fileName) | |
// pattern_1. replace preload to prefetch | |
// const regex = /as=\"fetch\"\ rel=\"preload\"/g | |
// const replacedContent = content.replace(regex, 'as="fetch" rel="prefetch"') | |
// pattern_2. remove code | |
const regex = /<link as=\"fetch\" rel=\"preload\" href=\".*\.json\" crossorigin=\"anonymous\"\/><link as=\"fetch\" rel=\"preload\" href=\".*\.json\" crossorigin=\"anonymous\"\/>/g | |
const replacedContent = content.replace(regex, "") | |
await writeFile(fileName, replacedContent) | |
} | |
async function main() { | |
const htmlFiles = await getFileList( | |
path.join(__dirname, htmlFilePath) | |
) | |
return Promise.all( | |
_.map(htmlFiles, async fileName => { | |
console.log(`replace tag: ${fileName}`) | |
await replaceTag(fileName) | |
}) | |
) | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment