Skip to content

Dev server serves stale JSON when functions use import statement for JSON #6094

Open
@coaxial

Description

@coaxial

Describe the bug

When a Netlify function imports a JSON file with import myJson from './my.json', it will only ever return the contents of ./my.json as they were when the dev server was started, even after ./my.json is modified while the server is running.

While import statements only run once when executing the file, Netlify functions are AWS Lambdas which are expected to be executed from scratch and thus run the import statement with every execution. But netlify dev doesn't behave this way, only reloading the function's code when the actual code is changed.

This is an issue in development: I'm working on translating an application, and the translations go into a JSON file. A netlify function returns the corresponding language's JSON which is loaded with an import statement. The result is that as I add new strings to the JSON file, the function only ever returns the JSON file as it was when the dev server was started. The only way is to kill and restart the dev server.

Steps to reproduce

  1. Create a JSON file with these contents: {} at ./my.json
  2. Create a Netlify function with these contents:
// netlify/functions/jsonTest.js

import myJson from './my.json'

const handler = event => {
  return {
    headers: { "Content-Type": "application/json" },
    statusCode: 200,
    body: JSON.stringify(myJson)
  }
}

export { handler }
  1. Start the dev server with netlify dev
  2. curl localhost:8888/jsonTest
  3. Get {} as the response
  4. Without stopping the dev server, overwrite ./my.json with these contents: {"netlify":"rocks"}
  5. curl localhost:8888/jsonTest
  6. Get {} as the response, instead of the current contents for ./my.json
  7. Kill the dev server
  8. Start the dev server again with netlify dev
  9. curl localhost:8888/jsonTest
  10. Get {"netlify":"rocks"} as the response

Note that every once in a while, it will actually reload the function and re-run the import statement so you'll get the updated contents for the JSON file. But most of the time, it doesn't and serves stale contents

Configuration

[dev]
  autoLaunch = false
[build]
  command = "astro build"
  functins = "netlify/functions"
  publish = "dist"
[functions]
  node_bundler = "esbuild"

Environment

System:
OS: Linux 5.15 Linux Mint 20.3 (Una)
CPU: (8) x64 Intel(R) Xeon(R) E-2134 CPU @ 3.50GHz
Memory: 12.83 GB / 46.93 GB
Container: Yes
Shell: 5.0.17 - /bin/bash
Binaries:
Node: 18.15.0 - ~/.nvm/versions/node/v18.15.0/bin/node
Yarn: 1.22.19 - ~/.nvm/versions/node/v18.15.0/bin/yarn
npm: 9.8.1 - ~/.nvm/versions/node/v18.15.0/bin/npm
pnpm: 8.9.2 - ~/.nvm/versions/node/v18.15.0/bin/pnpm
npmPackages:
netlify-cli: ^16.8.0 => 16.8.0

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @serhalp@coaxial

        Issue actions

          Dev server serves stale JSON when functions use `import` statement for JSON · Issue #6094 · netlify/cli