Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

is it possible to create a global var that's shared between pages? #203

Closed
bhurlow opened this issue Nov 5, 2016 · 12 comments
Closed

is it possible to create a global var that's shared between pages? #203

bhurlow opened this issue Nov 5, 2016 · 12 comments

Comments

@bhurlow
Copy link

bhurlow commented Nov 5, 2016

No description provided.

@impronunciable
Copy link
Contributor

// myglobal.js

export default 42
// pages/a.js

import glob from '../myglobal'

glob // 42
// pages/b.js

import glob from '../myglobal'

glob // 42

@ugiacoman
Copy link

Isn't this what props are for?

@malixsys
Copy link
Contributor

malixsys commented Nov 5, 2016

See the redux example
https://github.com/zeit/next.js/wiki/Redux-example

@nkzawa nkzawa added the question label Nov 5, 2016
@nkzawa
Copy link
Contributor

nkzawa commented Nov 5, 2016

// myglobal.js

export default 42

btw, this wouldn't work on client, when the value was mutated.
Even if two pages requires a same module, the module becomes two different objects on each page, because of code splitting.

@CompuIves
Copy link
Contributor

The Redux example is an example with one page. I think the initial question is if it's able to switch between different pages while keeping a global persistent state. I don't think that's possible yet, but may be possible after #25

@sedubois
Copy link
Contributor

sedubois commented Nov 7, 2016

@CompuIves, @malixsys is right, the Redux example shows how to keep state across page changes, through the window object (although it's quite hacky, IMHO). Similarly for Apollo: https://github.com/sedubois/realate/blob/master/containers/Apollo.js (see #106).

IMHO a better central management of state is needed, as these issues keep popping up.

@bhurlow
Copy link
Author

bhurlow commented Nov 7, 2016

something like a registry API might be appropriate for this e.g.

import Registry from 'next'
let session = await Registry.get('session')

@bhurlow
Copy link
Author

bhurlow commented Nov 7, 2016

Essentially looking for a way to get around the code splitting to store application wide settings. Will try extending the window object but would much prefer a server backed solution instead

@malixsys
Copy link
Contributor

malixsys commented Nov 7, 2016

You can do all the server-side you want, although a bit difficult at this moment until #25
just check for req === true

@eezing
Copy link

eezing commented Nov 7, 2016

@bhurlow

Just to clarify:

  1. Can you give a real world example of an application wide setting?
  2. Do application wide settings ever change durning runtime? If so, do they persist across runtime sessions?

@bhurlow
Copy link
Author

bhurlow commented Nov 7, 2016

@eezing sure,

  1. I suppose the obvious example is a session object stored in memory. In a traditional app a global could be referenced and potentially changed from multiple places, but the code splitting (⚡️ ) of next makes this more obscured. Or even something more trivial like a global counter value shared between pages...
  2. no persistence, just talking about the lifespan of the node.js process

Like @malixsys mentioned though, I can leverage the getInitialProps here and be just fine until #25

however it could be interesting to expose access to globals from the client as well, but that might be another can of worms

@arunoda
Copy link
Contributor

arunoda commented Dec 2, 2016

Guys, this is a pretty ugly bug and we fixed this with v1.2.x.
Here's a an example app: (source, live)

@arunoda arunoda closed this as completed Dec 2, 2016
@lock lock bot locked as resolved and limited conversation to collaborators May 12, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants