Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Global variables getting cleared in DEV environment due to recompilation #44330

Open
1 task done
ropmansk opened this issue Dec 24, 2022 · 2 comments
Open
1 task done
Labels
bug Issue was opened via the bug report template.

Comments

@ropmansk
Copy link

ropmansk commented Dec 24, 2022

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
  Platform: win32
  Arch: x64
  Version: Windows 10 Home
Binaries:
  Node: 16.15.1
  npm: N/A
  Yarn: N/A
  pnpm: N/A
Relevant packages:
  next: 13.1.0
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0

Which area(s) of Next.js are affected? (leave empty if unsure)

No response

Link to the code that reproduces this issue

https://stackblitz.com/edit/vercel-next-js-xfenwd?file=pages/api/setMessage.js

To Reproduce

Just call the setMessage API method, after that call getMessage method

Describe the Bug

I have a basic in memory storage object

memoryStorage.js

var storage = {};

export default {
    setMessage(message) {
        console.log('Setting message: '+message);
        storage.message = message;
    },
    getMessage() {
        return storage.message;
    }
}

And API handlers to set and then get the global message
getMessage.js

import Storage from '../../service/memoryStorage';

export default function handler(req, res) {
    let message = Storage.getMessage();
    console.log('Message from storage: ' + message);
    res.status(200).json({message});
}

setMessage.js

import Storage from '../../service/memoryStorage';

export default function handler(req, res) {
    Storage.setMessage('Hello World');
    res.status(200).json({status: 'ok'});
}

In production build everything works as expected, the problem occurs only in dev. When I request the setMessage API and getMessage after that, message in a HTTP response of getMessage will be ,,undefined" instead of ,,Hello World". This happens because of the lazy compilation in DEV. When you try it the second time, it is going to work because the compilation has already finished.

Expected Behavior

getMessage API to return ,,Hello World" instead of undefined

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

@ropmansk ropmansk added the bug Issue was opened via the bug report template. label Dec 24, 2022
@slice
Copy link

slice commented May 30, 2023

I'm also experiencing this while trying to keep a Postgres connection pool alive between requests.

If persisting something on the global object isn't supported, I'd at least appreciate some official guidance in the documentation re: creating database connections.

@nanzm
Copy link

nanzm commented Nov 29, 2023

#45483

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Issue was opened via the bug report template.
Projects
None yet
Development

No branches or pull requests

3 participants