-
Notifications
You must be signed in to change notification settings - Fork 27.5k
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
Document how to do stuff differently server-side vs. client-side #1229
Comments
Would also be nice to know how to write ES6 code on the server side. |
@knipferrc Node.js already support ES6, only modules aren't supported. |
@sergiodxa but cant I use babel-cli or something to get all the import, export and module stuff somehow? |
@sedubois there's a slack channel? maybe one could add that to the readme too ;) |
@sakulstra there's a slack badge at the top of the README |
@knipferrc we want to provide a server API that you can run vanilla ( Honestly, transpilation brings performance down. Something that I think about often is that Next.js could have an opportunity soon to bring some sanity back to development by avoiding transpilation during development (or keeping it at a minimum)[1], and only doing it upon [1] This is supported by the fact that Node is now tracking V8 very closely, and almost everyone develops on a modern browser with great ES support |
I believe @bnb mentioned it being around 1,5 years out 😅 |
So ES6 import syntax is still quite far away. According to this very recent update, at least another year:
So hopefully it will arrive sometime in 2018, but in the meantime, I'd really like some option to just write the same code on server and client, this is a really annoying mental overhead to remember to import syntaxes. And some features like object destructuring would also be really nice to use on the server side. So is there any workaround for now at least, or any way in which one could contribute to making the proposed |
It is pretty simple to use ES6+ syntax on the server in development, following the description here. Basically just replace the start script with: I haven't quite figured out, how to deploy this though and it seems there are some issues with hot reloading of the server code and using nodemon, doesn't work out of the box either (using it leads to constant reloading). I'll have to look into this a little more later. |
Hey @MrLoh, just wondering if you found a clean way to implement this? |
@tconroy you need to use Babel to generate files in disk with the code of your server and then use |
@tconroy I ended up doing something like this: .babelrc: {
"env": {
"server": {
"presets": [["env", {
"targets": {
"node": ["current"]
}
}]]
},
"test": {
"presets": [["env", {
"targets": {
"node": ["current"]
}
}]]
},
"development": {
"presets": ["next/babel"]
},
"production": {
"presets": ["next/babel"]
}
},
"plugins": [
["root-import"],
["transform-object-rest-spread"],
["transform-flow-strip-types"],
["import", [
{"libraryName": "antd"},
{"libraryName": "lodash", "libraryDirectory": "/", "camel2DashComponentName": false}
]]
]
} Than my start script looks like:
Then I have a .env that contains Which is called first thing in import { config } from 'dotenv'
config() // load .env variables into process.env |
Worked great, thanks @MrLoh! |
Worth noting that it has to be referenced as Fixed by changing to: Index.getInitialProps = ({ query }) => {
return { query, browser: process.browser }
} |
This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
In Slack and in issues there comes repeatedly the question of how to do stuff differently server-side vs. client-side. I added a note in Wiki related to this: https://github.com/zeit/next.js/wiki/FAQ but it's obviously not clear enough.
I think it would be nice to add to README:
getInitialProps
you can useprocess.browser
var to check where you are (which AFAIK is better than!!ctx.req
because this one still returnstrue
on the first client-side render...?)componentDidMount
only runs client-side, so can add client-only code there. More generally, precise which React lifecycle methods are client-only.The text was updated successfully, but these errors were encountered: