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

Pass variable to _document.js from server.js #3367

Closed
1 task done
blackbing opened this issue Dec 1, 2017 · 4 comments
Closed
1 task done

Pass variable to _document.js from server.js #3367

blackbing opened this issue Dec 1, 2017 · 4 comments

Comments

@blackbing
Copy link

blackbing commented Dec 1, 2017

Can I pass some variable from create next? for example

const app = next({ dev: isDev, customProps: { GA_ID: 'UA_XXXXXX' });

then I can get this GA_ID variable in _document.js and render it.

I know universal-config provide another way to handle it, but it will be replaced in the build time. If I have several different environment. I have to build by different config. So I hope I can pass variable to render _document.js

related issue is #159 but as I mentioned above, universal-config will replace variable in the build time instead of run time. so I hope there is a better way to do that.

  • I have searched the issues of this repository and believe that this is not a duplicate.
@blackbing
Copy link
Author

@timneutkens thanks for your example. I understand you export env config to windows.__ENV__.
I use https://github.com/mozilla/node-convict to configure my env variable. Consider this example code.

var convict = require('convict');

// Define a schema
var config = convict({
  API_URL: {
    doc: "API_URL.",
    format: String,
    default: "127.0.0.1",
    env: "API_URL",
  },
});
// Load environment dependent configuration
var env = config.get('env');
config.loadFile('./config/' + env + '.json');

// Perform validation
config.validate({allowed: 'strict'});

It loads the file and do some validate. however it cannot be executed on client side. The env config should be decided on server side, and I can decided what I should export to client side. So I am curious about if I make this variable from create next.

const app = next({ dev: isDev, customProps: { GA_ID: 'UA_XXXXXX' });

Is it possible?

@timneutkens
Copy link
Member

@blackbing _document.js is executed only on the server. Only the API_URL environment variable is exposed https://github.com/zeit/next.js/pull/3371/files#diff-bc9a93f185c52580e4adb7607bc35ddaR5. You can put the convict code in _document.js and it'll work just fine 👍

@misuzu
Copy link

misuzu commented Jun 15, 2018

@timneutkens
When running like this it does not work properly:

npm run build
API_URL='https://example.com' npm run start

This is needed when building on CI server and running resulting build on many environments with different configuration.

@timneutkens
Copy link
Member

@lock lock bot locked as resolved and limited conversation to collaborators Jun 15, 2019
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

3 participants