Site: fluffy-pony-237262
I’m trying to expose build time information to include it to the web pages. To this end, I have created the following plugin:
import * as fs from 'fs'
import * as path from 'path'
export const onPreBuild = ({ constants }) => {
const commitRef = process.env.COMMIT_REF || 'unknown'
const branch = process.env.BRANCH || 'unknown'
const buildTime = new Date().toISOString()
const buildInfo = {
commitRef,
branch,
buildTime
}
const filePath = path.join(constants.PUBLISH_DIR, 'build-info.json')
fs.writeFileSync(filePath, JSON.stringify(buildInfo, null, 2))
console.log(
`Build info written to ${filePath}: buildTime=${buildTime}, commitRef=${commitRef}, branch=${branch}`
)
}
Now the public folder has a file that contains the relevant information.
In React (Remix to be specific), I’m trying to read the content of the file directly without separate HTTP fetch call with a code snippet like this:
const filePath = path.join(process.cwd(), 'public', 'build-info.json');
const buildInfo = JSON.parse(fs.readFileSync(filePath, 'utf8')) as BuildInfoFile;
(The specific location for this is in root.tsx
.)
I have a couple of questions relating to this:
- Is it a good decision in trying to avoid extra HTTP fetches when building the page?
- What would be to correct folder to write this kind of file?
- What is the correct way of referring the folder in question?