I just deployed my first nextjs site to netlify and it has been an amazing experience except for a failure in an unexpected place.
I have newsletter pages of the form:
The code uses the number at the end to read a file of the same name in a subfolder, via code like this:
//the filename where the newsletter content is
var filename = isLocal ?
`./public/newsletter/${params.number.padStart(3, "0")}.html` :
`/var/task/.next/newsletter/${params.number.padStart(3, "0")}.html`;
let content = fs.readFileSync(filename, 'latin1')
If you try this at this moment, it fails with:
Error: ENOENT: no such file or directory, open ‘/var/task/.next/newsletter/055.html’
The file newsletter/055.html
is in my public folder, and is deployed to the server, because if you request it directly in the browser it works. And on localhost it can load the file and the request succeeds. But on the server, I cannot read the file, and I’ve tried a variety of paths to no avail.
What am I doing wrong here, and how can I fix it?
@jfrank14 I can’t speak to the “Next.js way of doing things” or any tooling or helpers that may exist, but your site is almost certainly being deployed to two locations.
Some is deployed to the CDN, some is deployed as Serverless Functions (AWS Lambdas).
The code deployed to the functions only has access to what is deployed in its bundle.
It does not have local file path access to files that were deployed to the CDN.
You can use included_files
, set in the File based configuration, to specify files to include in the function bundle.
See:
@nathanmartin Thanks for the link. That pointed me in the right direction. Doing this did fix the issue:
[functions]
included_files = ["public/newsletter/**"]
What is really strange, though, is that this error only happened when I tried to read the html content file for a given newsletter, like 026.html
. I have another file that lists all of the newsletters, and accessing it like this worked:
export async function getNewsletters(): Promise<IEnumerable<INewsletterRow>>
{
return (await toJson('./public/newsletter/newsletters.csv') as INewsletterRow[]).reverse().toLinq()
}
Does nextjs parse the source code, see a fixed file reference like this, and include the newsletters.csv file even without me asking for it to be included?
As prefaced…
I don’t work for Netlify and I don’t use Next.js.
For a Next.js specific answer your best bet would be asking their community.
Netlify staff might be able to provide some insight on if there is any ‘magic handling’ on their end or not.