-
Notifications
You must be signed in to change notification settings - Fork 28.9k
Description
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