53: Embrace the PHP with Next.js, Featuring Tim Neutkens
Have you had the privilege of working in PHP?
If you haven't let me tell you:
You can have a dynamic website just by putting a single PHP file on a host, anywhere.
It's magic
For us React developers, everything is a lot more complicated.
We want server-side rendering for Google crawl-ability,
Hot Module Replacement for quick feedback in development, and code-splitting to get quick initial page loads for users.
None of that is easy to implement.
But there's hope.
The team at Zeit wants you to have all that but with the simplicity of that beautiful PHP workflow.
And they've done it.
We sit with Tim Neutkens, lead developer on Next.js, an open source framework, for react, by Zeit.
He tells us how you can get back to that beautiful, fun PHP experience but with all of the benefits of SSR, HMR, AMP, and so many more initialisms.
I'm so excited to share this chat about Next.js the next-live of static site generation.
Featuring
Links
- Next.js 9 is out now!
- Next.js — The React Framework
- next/dynamic
- styled-jsx — Full CSS support for JSX without compromises
- Dynamic Pages with Next.js
- Tech Choices I Regret at Spectrum — Blog — by Max Stoiber on why he would have used Next.js
- Tech Choices I Regret at Spectrum — Talk
- Next for Next.js — Tim Neutkens at React React Amsterdam
- facebook/codemod
Sponsors
GitPrime
GitPrime turns historical git data into easy-to-understand insights and reports that help engineering teams succeed. gitprime.com
G2i
G2i is a hiring platform for remote developers devoted exclusively to React and React Native. We vet, you get hired. g2i.co
Transcript
[00:00:00] Michael Chan: This is React Podcast. I'm Chantastic. Hey, React friends. This week we sit down with Tim Neutkens, lead developer at Zeit, to talk about his work on Next.js. Next.js is a React framework for building apps. It's packed with tons of goodies like server-side rendering, hot module replacement, and now support for AMP, Accelerated All that, and it's wrapped in the simplest developer workflow I've seen since getting my start with PHP. Now that's saying a lot for JavaScript. I'm excited to get Tim on the show to talk about Next.js. This episode is sponsored by 2 great sponsors, GitPrime and G2i. I'll tell you about GitPrime now and G2i a little later in the show. GitPrime wants to give listeners of React Podcast a free book. And not just an ebook, a printed book with beautiful illustrations. The book is 20 Patterns to Watch for in Your Engineering Team. It's a field guide to help you recognize achievement, stop bottlenecks, and debug your development process with data. So what does that mean? Every team can improve their development process, no matter how good they are. Something that always bothers me is when I wait for days to have a pull request reviewed only to have a teammate rubber stamp it with, looks good to me, or maybe they miss the whole point entirely of the PR and just point out pedantic syntactic nonsense. You know, the easy, meaningless stuff. 20 Patterns to Watch for in Your Engineering Team will help you identify and name these failing patterns so you can grow as a team, work happier, and build a product that builds a business. Visit gitprime.com/20patterns, that's the number 20 patterns, and get this free book. Immediately as a digital download and then print if you wish. And let me tell you, you will want this in print because this thing is beautiful. Or you can just admire it on your screen and save a tree. GitPrime, engineers build business. Hey Tim, how are you doing today?
[00:02:21] Tim Neutkens: I'm good.
[00:02:21] Michael Chan: I want to welcome you to the show. I am so excited to have you on. I'll tell you exactly why in a second. But um, for anyone who is not familiar with your work, Uh, tell us about what you do, why you do it, why you love it, and uh kind of how you got there.
[00:02:39] Tim Neutkens: Right, so I'm Tim Neutkens, um, and I'm the lead maintainer of Next.js, which is a framework for uh pre-rendered uh React applications. So that means like server-side rendered or uh static rendered, um, and I lead the team that builds out Next.js at Zeit. Which is a cloud hosting company that allows you to host, for example, Next.js, but also other frameworks and other languages in a serverless manner.
[00:03:12] Michael Chan: How long have you been working with Zeit?
[00:03:14] Tim Neutkens: For 1.5 years now, I think.
[00:03:18] Michael Chan: Awesome, awesome.
[00:03:19] Tim Neutkens: I started in December of 2018, or no, 2017 actually. Yeah, so I've basically been maintaining uh Next.js ever since it came out uh in my free time as a hobby. Um and then eventually I ended up uh getting an offer from Zeit to work on uh Next.js full-time uh and build it out more uh and just spent more time on it, basically.
[00:03:47] Michael Chan: That's amazing. I love these stories of open source where you kind of get into something just because you really align and love the project, or align with and love the project, and how that creates opportunities for, you know, for for jobs, for speaking, for, you know, for whatever. How big is- how big is Zeit now, and how big is the team that is working on on Next?
[00:04:12] Tim Neutkens: Great question. Um, we currently have around 40 people at Zeit, um, and 5 of those are on my team. Uh working on Next.js.
[00:04:23] Michael Chan: Wow, wow, that's a pretty big percentage. Are they working on it full-time too, or um do they have shared responsibilities with other parts of Zeit?
[00:04:31] Tim Neutkens: So 3 people are working on it full-time, um and then uh 2 are the in-between of hosting Next.js on Now, um and making that whole experience uh the best it can be.
[00:04:44] Michael Chan: Wow, awesome, awesome. So there's so many, so many questions that I want to ask you, um so but first of all, I I want to get my bias out of the way. I love Next. I find it an indispensable part of of my work. I work on um design systems at my company. And it is just the the perfect tool for me to to kind of help me do that job really well. Um, could you describe to me just what the the big design goal of Next is? Like what is the what's the inspiration there? Why is it such a different product than some of the other, you know, boilerplates or kind of generators that exist in the creating of React apps?
[00:05:31] Tim Neutkens: So the- okay, so the main difference is that um we try to put you in the pit of success uh for building React applications. So we uh try to optimize as much as possible to let you focus on uh what you want to do, which is build your application code and not the build tools around it. And then also optimize the code that you produce in a way that you don't have to go back and like change things. For example, in the compilation side of things and like the, I don't know, for example, Next.js does hot module replacement for you automatically, which is a really hard issue to tackle on your own. So yeah, Next.js basically helps you be more productive at building React applications.
[00:06:21] Michael Chan: Now, why React? There's there's a lot of um there's a lot of client-side uh lib libraries now that do similar things to to React. If you were to start it over today, would you still choose React or would you choose something else?
[00:06:35] Tim Neutkens: I would definitely go with React. I think it's not like there's a lot of really good uh really good frameworks out there right now, uh especially client-side frameworks. Uh for example, You have React, Vue, uh Svelte, which just came out with another version yesterday. But React has been has become like the integral part of pretty much everyone's developer workflow. Um and it has certain properties that allow us to do certain kinds of optimizations. While there there are like alternatives in other languages, for example, um In Vue, there is Nuxt, which is based on Next, but also uh Svelte has Sapper that do very similar but different uh kinds of workflows, basically.
[00:07:25] Michael Chan: So one of the key features, as far as I read it, of Next is that, you know, while you could get a client-side library out of just- or sorry, you get a client-side rendered site out of any application. Part of the the thing that Next gets you by default is really good server rendering, which is a a really hard problem to kind of bolt on- or a hard thing to solve afterwards. So if you start with a you know purely client-rendered React application, it's kind of hard to bolt on uh server rendering after the fact. And it seems like one of the core features of Next is that you solve both of those problems in tandem. Uh, is that- am I understanding that right?
[00:08:11] Tim Neutkens: Yep. So, basically, uh, what Next.js allows you to do is, um, build server-side rendered applications, uh, but that are also, for example, client-side rendered. So, what we do is we first serve render the whole, uh, page. Um, so, we basically generate the initial HTML from your, um, React application. And then put that out to the client side. Then the client side loads, and it will load all the JavaScript for your application. And then we hydrate that, which is basically the process of like attaching the onclick events and that kind of thing. Um, and then it's a fully functioning React application on the client side.
[00:08:53] Michael Chan: Wow. Now, you're you're doing a lot here. And you mentioned uh hydration. And I I think that I understand that that you're splitting all of the, um, you're doing code splitting on all of the routes. Now, are you just using regular React under the hood? So, you know, ReactDOM.hydrate and React.lazy, or do you have some extra magic sauce that, that you have to build to make all those things work?
[00:09:18] Tim Neutkens: Right, so by default, uh, what you do is when you build a Next.js application, you create a pages directory, uh, that in very, uh, in many ways is very similar to PHP, for example. You create a Um, a document or a folder. You put in files that are uh React components. So you create, for example, about.js that exports uh a React component. And then Next.js will automatically server render and client render that React component, uh but also does routing for you. So the about.js becomes /about on your application, basically. So, we use React, uh the just vanilla React, uh using uh react um .hydrate, for example, for— react-dom.hydrate for uh hydration. Um, using— uh for code splitting, we do— we basically allow you to do 2 things, which is we code split by default per route, meaning that if you change one route and don't change another route, and it breaks, it will only break that one route.
[00:10:27] Michael Chan: Hmm.
[00:10:28] Tim Neutkens: But next to that, there's also, for example, React.lazy, which allows you to do component- on the component level, do lazy loading of components or splitting out code. Um, for that, uh, you can use React.lazy, uh, but it doesn't work on the server side. So, you'd get, like, a loading state, for example. Um, but we provide a uh a a wrapper library that is called uh NextDynamic, which is which has pretty much the same API as uh React.lazy, except what it does is it optimizes for- for example, if you do import some component uh inside of NextDynamic, it will inject the script tag uh to preload that component when you do server-side rendering, for example.
[00:11:20] Michael Chan: Oh, wow.
[00:11:20] Tim Neutkens: So it does a little bit more than just React lazy. So under the hood, it would use React lazy. It doesn't currently because it doesn't support server-side rendering right now, but eventually it will. Yeah, that's how it works.
[00:11:32] Michael Chan: Cool. That's one thing that I really love about the the way you've built up this framework is that you have these really cool ergonomics. Um, and I I love that PHP analogy, 'cause that's totally what it feels like. Like I feel like a kid every time I'm using using Next, because it just reminds me me of that early time in the web where it was like I just put a file up there and started working. And it was like super fun and super direct and there wasn't like a big framework sitting in between me and my intent. But you handle those- I guess those cases where you need a different solution for server rendering with such respect for React. And so, like you said, you have this wrapper around React lazy that allows you to get Um, you know, to get those features in both places. Um, what are some other areas? Um, I know that from from my knowledge, and I have very limited understanding of the scope of Next, but there's two places that you've kind of added some some features, which is um Next Link and um also getInitialProps. Could you tell me about those and any other things that, you know, someone who wants to be successful in Next would would need to be aware of?
[00:12:44] Tim Neutkens: Right, so uh to start off, um since you have pages, since Next.js also has its own routing layer, uh which is um server rendered, but also uh it works on the client side for um single page application route transitions basically. So similar uh to doing, for example, if you use Next Link, uh which is a link component that if you use it, you point it to another page, but what happens is that page is not server-side rendered, it's actually fetched on demand and then rendered on the client side instead of on the server side.
[00:13:24] Michael Chan: Mm.
[00:13:25] Tim Neutkens: And then to go into getInitialProps, getInitialProps solves the data fetching issue, which means that uh in a React in a React library, you generally, or in a React project, You generally want to do data fetching of some data, like any data. For example, uh you have a blog and you want to get the posts, for example. Uh then you use getInitialProps at the top level, so at the uh pages level, uh to do data fetching uh from any source. So it's an asynchronous JavaScript function where you just kind of wait for data to be loaded before React starts rendering. So we've added in this extra API basically for doing data fetching.
[00:14:09] Michael Chan: I love it. I love it. Now, one thing that I also love is the way that you handle CSS. Um, Next, uh, I believe the package is Next CSS. I could be be getting it wrong. Um, but there's some really great ergonomics about including that CSS like just at the places you need it. You also have this library um called styled-jsx. Uh, can you tell me about how that fits into uh Next.js and and also the broader scope of CSS-in-JS libraries?
[00:14:38] Tim Neutkens: Right. So, um, styled-jsx is basically a very thin layer, um, that we added in by default for Next.js, which allows you to write, um, CSS like you would normally write CSS, but then do it inside of components. Um, and they're all scoped to that component. So, uh, you can compare it to, for example, styled-components or Emotion, but with a less- like, with a simpler API. So, it's just style JSX and then the styles that you want to write, basically.
[00:15:14] Michael Chan: It's basically like a like an inline style tag, but it's it's inherently scoped.
[00:15:19] Tim Neutkens: It's basically it looks like an inline style tag, but it's not. That's basically how it works.
[00:15:24] Michael Chan: Yeah. I I love that. It fits really well with the kind of I don't know, like the subtle deceptions that React has inherently. Like you're writing this thing and it feels like a certain way, but it's actually not at all how it works under the hood. And the way it works under the hood is like hyper-optimized and exactly how you want it to. Um, but you get to keep that really nice developer uh experience.
[00:15:45] Tim Neutkens: Right. Because styled-jsx, for example, uh compiles into uh the style tag that is then combined uh at the server-side rendering level. Um, so it's just one style tag that comes out, basically. And then also that works on client-side rendering, for example. Um, but even uh if you don't want to use styled-jsx, for example, you can use any uh CSS-in-JS library with Next.js. So you can use styled-components, emotion, uh basically every library that's out there. Um, and we have an examples directory that has examples for pretty much every possible uh library that you want to use.
[00:16:24] Michael Chan: I'm glad you mentioned that, because that is one of my favorite things about Next is it is, uh first of all, it's beautifully documented, uh kudos for that, but it has so many great examples as well. And sometimes those are even better than the documentation because you get to see the real code that makes the thing happen. And so you just find the example that's, um you know, like the thing that you're trying to achieve and basically just copy and paste, you know, the solution. It's it's amazing.
[00:16:51] Tim Neutkens: Yeah, it's it's super great. Um, it's also good to note that most of these examples, because we have, uh I think from the top of my mind, like 190 right now, uh which are uh mostly contributed by uh just users that are using Next.js in production or building their applications uh and wanted to contribute back to the framework, basically.
[00:17:13] Michael Chan: That's awesome. I I love seeing community Efforts like that. Now, one thing that I'm super curious about is, as we've kind of established already, there's there's a big gap in thinking between like a client-only uh application and a server-only application. And Next is doing a really great job of marrying those uh in a way that you don't have to think about it a whole lot. But it doesn't mean that the complexity goes away. And it doesn't mean that you're not going to do things on the server specifically. And one of those things is is like dynamic routing. So when I think about dynamic routing, I'm thinking about something like unpackaged.com that Michael Jackson does. Where, you know, you you could— it's just a wrapper around— or like kind of a directory view of everything that's on npm. Now, you wouldn't want to generate static routes for all of those things. So how would you take a a Next app- And kind of move it into or uh engineer it so that it could support a a need like unpackaged.com?
[00:18:19] Tim Neutkens: Right. Great question. Uh it's something that we get a lot of feedback on, uh because we currently have an API that where you have to create uh a custom server or do custom routing logic uh that then maps the incoming request to another— uh to to basically a page plus a query string. Right? So what you do is you take your, for example, serverless routing layer or your custom server or anything basically. Uh it gets the URL. Uh it does then it does pattern matching outside of Next basically. And then uh you move it into Next. Like you say uh next.render basically. Uh and then this route and these parameters are coming in basically. Uh, but we're currently working on a new way to do uh dynamic routing, which is based on the file system. You create a directory uh that is marked uh in a specific way that then allows you to do these um more— like to do segmented routing, basically. Like, for example, uh you do /blog/ uh the slug, for example. Of the post. And then we automatically route you to that. Um, once the URL comes in, we automatically match that and inject that parameter into getInitialProps, for example, so that you can fetch based on that value.
[00:19:47] Michael Chan: Awesome. It's, it's exciting to see that you guys are continued- continuing to work, uh, work through some of these things, uh, given some of the, the feedback. I want to transition into a little bit about what it looks like to maintain uh Next.js. Um obviously, you have a a ton of sites using it. How how many sites do you estimate are using Next.js now, at least in public places that you can index?
[00:20:15] Tim Neutkens: Right. So, last time I checked, it was over 15,000.
[00:20:19] Michael Chan: Oh, my gosh. That's insane.
[00:20:20] Tim Neutkens: And that goes from, like— uh actually, I just checked right now. It's like 16,500 or something. Um more than that, basically. Uh, but it goes from, like, very large scale, like, crazy scale, 6 billion users in Q1, um, to very small websites that are never, uh, accessed, for example, like personal websites or that kind of thing. Sure. So-
[00:20:46] Michael Chan: Yeah.
[00:20:47] Tim Neutkens: Yeah, it goes across, like, the whole spectrum. Like, uh, we have, I don't know, like, Lego and Marvel and- IGN, if you know that website, it's a very big gaming website.
[00:20:58] Michael Chan: Yep, yep.
[00:20:59] Tim Neutkens: They're currently migrating everything to Next.js. So more and more we see these companies, uh like Hulu or AT&T, or I don't know, Binance, which is a very large cryptocurrency company, um that are all moving towards Next.js, um and are using it in production in very large scale.
[00:21:18] Michael Chan: What does it look like to maintain a project of this scale. Uh, obviously, you know, it's it's one thing to to be working on and maintaining a project um that that is has a much smaller scope, but this really does uh have, you know, wide-reaching a wide-reaching audience. You know, like you said, there's like huge companies with, you know, tons of page views, and then, you know, kind of just, you know, little guys like like me who are just like putting up a design system page that only our team really looks at. Um, what are some of the things you've learned about maintaining this type of project?
[00:21:53] Tim Neutkens: Really good question. Um, I think that the one takeaway that I always had is that uh it's really important to talk to uh users. So for example, uh I end up talking to a lot of users of Next.js from like very large scale to very small websites. Uh just to gather their feedback about like issues they're running into or things that we can improve, um to provide the best possible developer experience. For example, in development, but also like, uh for example, companies saying like, oh, our builds are very slow, um and then figuring out like the reason why that is. Mm-hmm. But yeah, it goes all over the place. We get like a lot of issues, for example, uh that are sometimes not issues of Next, but of like integrations with other frameworks or other libraries. Um, yeah, that kind of thing.
[00:22:48] Michael Chan: That's cool. Was Next really the first project of this size that you've maintained, or did you cut your teeth on another open source project?
[00:22:56] Tim Neutkens: Um, no, this is the first. Uh, I used to help maintain Hyper, which is this um terminal that Zeit also built, uh, which is a terminal that is used by a lot of people, um, And it's built on Electron, so it's like built on HTML, CSS, JavaScript. Actually, it uses React. Um, it's how I learned React, uh, fun fact. Um.
[00:23:19] Michael Chan: Wow.
[00:23:20] Tim Neutkens: Yeah.
[00:23:21] Michael Chan: That's a fun way to learn React. I feel like most people learn it in the client, but you're learning it to run a terminal application. Yeah. That's pretty amazing.
[00:23:29] Tim Neutkens: So just by contributing, I learned um uh React and Redux because it also uses Redux under the hood. So yeah, uh so I helped on Hyper, but then uh besides that, I was doing mostly PHP work uh at an agency here in the Netherlands. And I I was mostly working on like Magento websites, uh like with PHP. So that's why like Next.js attracted me a lot when it first came out, uh because it's very similar to PHP, but then it's not really. Um yeah. And there's a lot of optimizations that you can do. Basically on the Next.js level, we can do more optimizations than, for example, on PHP applications. Because we also control the client-side JavaScript. And the whole client-side experience, basically.
[00:24:15] Michael Chan: Yeah, I think that's the biggest difference and and something that is so— I guess can't be overstated. Is there's something so beautiful about just having one experience where you're doing dynamic and static uh asset generation. Like it's kind of hard to explain, but even today, so many of the applications that the the applications that I actually work on for work, we have the kind of server-generated phase, you know, so I work on Rails apps that have these kind of complex React components, but there's like there's two brains that you have to kind of think about all the time. And something beautiful about Next is you have, it's all just the same thing. Like you just write it as it's dynamic. And then Next figures everything out for you.
[00:24:58] Tim Neutkens: Yeah, exactly. So, one of the main issues that you have if you don't do isomorphic or universal rendering is that uh you end up writing server code and then client code that mimics it. So, for example, if you take a very simple example, uh you build a PHP website that is then like hydrated using uh React, you have to like copy all the React, um, all the HTML markup into your React component and then like hope that it goes together, for example. So yeah, that's one of the beautiful things about doing prerendering using, uh, React, for example.
[00:25:35] Michael Chan: So something that has, um, been kind of in the popular ear of the React community right now is, um, Max Soiba of, um, Spectrum.chat, which recently got acquired by GitHub. Has been talking about the things that he would have changed if he was building Spectrum.chat today. And he's been talking a lot about how he would have started started with Next. Now, that's that's kind of impressive given the fact that he kind of cut his teeth on open source in um Rec Boilerplate. Um, how does that feel as a maintainer to to kind of have someone like Max? Yeah. Who has been part of a successful startup say, hey, if I were to do this again, I would just use Next?
[00:26:22] Tim Neutkens: So, Spectrum is actually a really interesting uh story because they started out as a Create React App application, but then they eventually decided that they needed server rendering. Um, and then basically Max added in uh server rendering on top of Create React App, like basically rolling your own thing. Uh, because how hard can it be, right? That is literally what he said in his talk at React Amsterdam 2 weeks ago. Um, but it's actually- there's a lot of nuance in doing server-side rendering or doing pre-rendering, um, and then like hydration, that kind of thing, data fetching. Um, but basically, uh, it's not just data fetching and hydration, it's also that, for example, uh in Next.js, Next.js allows you to scale to many pages. So it's not just that you have, for example, you create your React app, you have one bundle with like, it's basically like one page in Next.js. Yeah. So if you build out your whole application in that, you end up importing a lot of uh code that isn't needed for, uh for example, your I don't know, your uh post view in Spectrum. Um but also what Next.js allows you to do is, uh because it's split out on the pages level, uh all these pages are unique, like sort of sub-applications. That's how people use them sometimes. And it allows you to do, for example, scale out server rendering for these individual pages. So, and then also it doesn't load the map of all possible routes on the client side. So, if you go from one page to another, uh we don't know yet what code will be there, and then it loads that all automatically for you. So, it never loads too much code, basically. It only loads the code that's needed for your application, uh for that page of your application.
[00:28:19] Michael Chan: I want to cut in here and tell you about another great sponsor, G2i. Do you need proficient React talent fast? Have you had trouble finding qualified developers that can hit the ground running? Truth is, hiring is an expensive process. Just finding candidates can slow your team's productivity to a grinding halt. And there's a huge gap between resume and React proficiency. You could roll the dice, test your hiring abilities, and put your goals at risk, or you could hire G2i to find the perfect developer for you right now. G2i finds the best React and React Native talent available and determines in which areas they're strongest. Engineers have been vetted through React-specific code reviews and technical interviews, and when you work with G2i, those scores and notes are provided to you upfront, saving you time, energy, and keeping your team focused on building the product, not finding a needle in a haystack. Let G2i solve your hiring problem for you. Visit g2i.co today and find a developer handpicked for your team and application stack, ready to go. G2i. We vet, you hire. It's that simple. So I want to— I have a ton of questions, but I want to uh kind of transition just a little bit into some questions that a lot of people asked on Twitter. And this is actually the first time I've asked people for questions. And there were some really great ones. So, I want to I want to give uh them a chance to ask their questions. Uh so, the first one is um Giuseppe asks, if you were to build Next from scratch today, uh what would you change?
[00:30:03] Tim Neutkens: I actually saw the question before uh you because I saw what you posted. Um
[00:30:08] Michael Chan: yeah.
[00:30:09] Tim Neutkens: Fun fact, Giuseppe is actually the person maintaining start-jsx. Which is part of Next.js.
[00:30:14] Michael Chan: Oh, okay, awesome.
[00:30:15] Tim Neutkens: And he's done a really good job, so I wanted to give him a shout out. Um, but if I were to go back and uh start over, what would I do? I would probably— uh there's this one thing in Next.js that I kind of don't like, or it's like suboptimal, which is that uh currently we have a link API. So you do next/link, you create a link, uh then you have a href. And it points to another page, right? Sounds easy so far. But then if you have dynamic routes, you have to do a href which points to a page in your pages directory. And then you have an as property that is basically the the route that you'll see in your browser URL. And then also the href has to have all the dynamic properties, basically.
[00:31:01] Michael Chan: Gotcha.
[00:31:01] Tim Neutkens: But on the bright side of things, we're currently working on a new API for doing linking.
[00:31:06] Michael Chan: Oh, cool.
[00:31:06] Tim Neutkens: Uh, that won't have this uh this same issue that uh we had before, basically. And it will also do, for example, um find dead— co- not dead code, but dead links. So, for example, if you linked to another page and you delete that page, right now, uh we we have no way of detecting that because we don't know, like, which pages are linking to each other. Uh, but soon we'll be able to know that because we'll have a graph of all the links between pages.
[00:31:38] Michael Chan: That's awesome. Is this something that people will be able to use in tandem with the current Link API and be able to migrate gradually toward, or is this something that will be like a whole a whole shift and you'll have to rework your app?
[00:31:51] Tim Neutkens: Right. So, uh, it's probably good to note that um we're very committed towards not breaking anything. So, for example, all our major releases are generally just Upgrades of, for example, React and Webpack and Babel and that kind of thing. So one good example is if you take the the first README of uh Next.js, which is uh before we even started implementing Next.js, which are basically like the the ideas that Next was built on. Um so like the first APIs like next/link and that kind of thing, they all still work um in exactly the same way as documented. Uh, even if you like compare that README to like what we have right now, it was mostly like additions to it, not like things removed. So-
[00:32:37] Michael Chan: That's quite an accomplishment.
[00:32:38] Tim Neutkens: Yeah, uh, it takes a lot of effort, uh, especially uh when we want to like do this kind of optimization um on top of the existing uh applications. But we're also committed to um not breaking anything because we use Next.js for uh most of the site products. So, for example, the site dashboard and that kind of thing. Uh, and those are over 400 pages.
[00:33:04] Michael Chan: Wow.
[00:33:04] Tim Neutkens: Uh, which means that if we break something, we have to go in and fix all these pages, um, which takes a lot of effort, of course, from our side also.
[00:33:14] Michael Chan: This is one thing I really love about the React project as well, is is that they're they're very committed to not breaking things for a lot of the same reasons. They have like, you know, fif- famously have like 50,000 or some components in the Facebook library and nobody wants to go back and have to, you know, apply some breaking changes to all those components.
[00:33:34] Tim Neutkens: Yes, definitely. So what we've been doing is similar to the React team, is that if we make a really like breaking change, which has only happened like I think once, uh which was that we removed uh a property that was automatically injected into pages, uh which sort of created a weird state of usage. Um, so we've created a code mod, which is a script that basically takes your code and then converts it into the new way of doing things, but automatically. So uh you can run it over your whole project and then it like just automatically fixes everything. So the React team does this, for example, uh for like sorting uh function names and that kind of thing. Uh but also like getting rid of— well, they had code mods, for example, for uh React.createElement uh versus like classes, right? So, you had createClass versus um uh the class like the class syntax itself. So, you could do a code mod for converting those, basically. So that you didn't have to manually write code for that.
[00:34:40] Michael Chan: Awesome. Awesome. I love it. I I love this trend of like code mods. It's It's it's been great. So uh another question, I think we've probably uh asked this, but this this came up a bunch and I'm kind of generalizing, but the general idea is like with Google getting better at indexing uh single page applications, when is it important to consider Next um versus just doing a client-side, like uh create-react-app um application?
[00:35:09] Tim Neutkens: Right. So. It's something that comes up a lot, actually. Uh, there's basically, like- Google is actually indexing JavaScript applications, but uh, it's a very tedious process, which means that they basically boot up Chrome, they go to your website, they index your— uh, like, the content that you can see. But if your content loads, like, too slow, for example, it can't index. Um, but also, it has a crawl delay. Which means that if you have a fully JavaScript application and you're running a news website, for example, or basically anything that has frequent updates of content, it will take at least 2 days or 3 days to 2 weeks for the renderer that Google has to catch up to your website, basically. Because what happens is once Google comes in, it basically renders your- it server renders your application. So it gets the HTML, it does like the initial, like, it checks the meta description, the title, all the content in your page, that kind of thing, right?
[00:36:17] Michael Chan: Yeah.
[00:36:17] Tim Neutkens: And then it puts that page into basically a line, a waiting line, or a queue that is as long as like all the all the things that it has to index. And then like eventually it goes to your uh website as in JavaScript. And then once that happens, it indexes it. Uh and then doesn't take into account all the meta tags. It does take into account like title and description and that kind of thing. Um but it's not 100% the same thing as doing uh full HTML uh server-rendered response.
[00:36:51] Michael Chan: That's interesting. So so while they're getting better, it's still not foolproof. Like it's it's there's not one-to-one parity between uh having a you know, having static assets and having a single page application.
[00:37:04] Tim Neutkens: Correct. So, it's probably also good to know that with Next, you can actually do SPA without doing server-side rendering using static export and then like only loading the the shell of the application and then the rest is like dynamically fetched in the similar manner that you do, for example, create React app or anything else. But I replied on the tweet that you you were mentioning uh with a talk from Google uh I/O from last year uh that goes very deeply into like how it works, um which is probably good to note in the uh podcast description afterwards.
[00:37:44] Michael Chan: Oh yeah, we'll definitely add that to the to the show notes for sure.
[00:37:47] Tim Neutkens: Sounds good. Anything else?
[00:37:49] Michael Chan: Uh yeah, so we have we have a bunch of them. So I'll try to I'll try to knock them up and let you hit them down. Uh Stefan asks, What are the long-term goals for serverless Next apps? And this is a little bit outside of my depth, so I'll let you you talk to this. Um, is it going to be a default even for non-NOW sites? Um, and then specifically, exporting static pages can be tedious with custom servers. Any plans to improve that?
[00:38:17] Tim Neutkens: Good question. So there's basically— uh to first explain, uh Next.js has basically two rendering targets for uh output for server rendering. That means that you have either serverless functions, so that is like Now v2 Serverless or uh AWS Lambda, uh or like Google Cloud Functions, or basically anything that does serverless rendering. Which means that every page that you have becomes a serverless function, uh that is then like independently deployed to the serverless platform. For example, it works really well with SiteNow. And what that allows you to do is scale out server rendering uh to basically infinite scale, because um you get individual pages that are executed. Uh they're very small, so you don't really have a cold boot um penalty. Uh or it's very, like- Okay. It's not noticeable. So, for example, the issue that serverless functions have is that they generally have a warm boot and a cold boot. Which is that a cold boot means that it basically gets your code, then boots up the the runtime, then executes it, and then replies. Um and warm means that uh that already happened, and then uh it stays around for a few minutes. Uh and those are like faster replies, generally.
[00:39:40] Michael Chan: Hmm.
[00:39:40] Tim Neutkens: But what uh the serverless target in Next does, together with, for example, Now, is uh it negates that cold boot by creating the smallest possible Lambda function that then boots up really fast. So the line between warm and cold is very blurred. Like, you can't even notice as a user.
[00:39:59] Michael Chan: Yeah.
[00:40:00] Tim Neutkens: Uh this means that your code is not running when it's not being requested. Uh so you're not paying for like anything. Say you have a website that is, for example, my personal homepage, where basically no one goes to, I think, uh because I don't have analytics, but- Say, like, no one goes there, uh I'm also then not paying for any uh server that is around or something. Um, like, I don't have a VPS that is, like, laying somewhere, always paying for it. Um, that kind of thing. Yeah. And then the the other thing that Next does is um server output, which then allows you to put it onto this VPS or, like, this other platform that um always runs the server's online, basically. Or like, I mean, um it's always uh running your code, uh which also introduces, for example, points of failure where you have to check if your asset is always online, uh because you're not sure if it's there, right?
[00:41:00] Michael Chan: Well, that's one really nice advantage. And I I really like the way you're going about solving this problem with uh with Lambdas. Is oftentimes, you can you can get a VPS, and it costs you some amount of money, and you just you you you're using that server all the time just for it to sit there. There and be ready in case, you know, the one person a week visits your site. Um, there are some free options, obviously, um, but a lot of times it's like they make it free by, you know, by only booting up the server, like keeping it warm, like after, you know, for a certain amount of time after someone visits it, which means that first person may have to wait like, you know, 20, 30, 40 seconds to get that that first response. Um, and I love this idea that you you're kind of like doubling down on these these Lambdas. Yeah. To to be able to make that experience great and keep the cost down for you know low traffic low traffic sites.
[00:41:48] Tim Neutkens: Yeah, and it's also for for example high traffic sites, um what we see a lot is that people for example create um applications that are uh built into for example Next server, um but then they have to scale out those servers um once traffic hits in. For example, like You post an article online and it goes on Hacker News or anything like else. And suddenly, like, you're being hit by, like, thousands of users per second. Um, and suddenly, like, your application can't take it anymore, or your server can't take it anymore, because it doesn't scale, um, by default. And serverless functions are basically the solution to this by just executing that code only when requests come in. And then automatically scaling it out uh for you on your behalf, basically.
[00:42:40] Michael Chan: Yeah, yeah. I love it. Um, let's see. I'm probably gonna skip a couple of these questions and maybe I can ping you in those tweets and we can we can answer them specifically with um with those people. Definitely. The the one that it seems really important to me, though, is um Chris asks, what is the the support story for hooks and caches right now in Next and kind of what's the plan going forward?
[00:43:02] Tim Neutkens: Do hooks and cache work? That is an interesting question. I just looked it up on Twitter just to be sure that I got that right. Okay, so let's let's answer at least one of those, which is, um, do hooks work with uh SSR or server-side rendering or serverless side rendering as we used to call it? Um, yes, they do. Like you can use hooks uh since React 16.8 and then Uh, if you use them, it just works. So, the way it works is, for example, if you use uh state, which is stateful, um, it's stateful, for example, uh, it just gets the default value on server-side rendering. And then when it hydrates, uh, you can do the setState, um, on the client side, basically.
[00:43:48] Michael Chan: Cool. Well, I want to give you an opportunity to tell us a little bit about what you're excited about in in what's coming up in the next versions of of Next. Uh I know that you just gave a talk where you released some cool features. So, tell us about what's going on there.
[00:44:03] Tim Neutkens: Right. So, last week was uh AMP Conf in uh Tokyo, uh where we launched uh AMP support for Next.js, which is— uh if you never heard of AMP before, it's this thing that uh started at Google but is now an open uh framework that people are working on in a similar fashion to Node.js, for example, using a steering committee and that kind of thing. Uh and it's basically, if you ever opened Google on mobile uh and searched for anything, uh you probably saw this lightning icon before, right? Mm-hmm. So, um that basically is an AMP-rendered page, which means that if you click on it, it loads inside of Google Uh, and it's very fast. So it runs through Google CDN uh automatically, and then like everything is optimized for that. So what Next.js now can do is render React components, uh server-side render them into AMP pages, which allows you to build very fast websites. Um, and AMP has some constraints to make sure that you create the the fastest possible website, basically. So, one example of this is we uh started using it for nextjs.org, which is the Next.js website. Uh and the documentation and the uh blog, for example, use it. And you can notice that it's almost as fast as doing uh client-side transitions, for example.
[00:45:33] Michael Chan: Crazy.
[00:45:34] Tim Neutkens: Because AMP makes sure that you uh don't do relayouts and, like, everything is there. Like, you don't see jumps, for example. Um, if you wanted to do, for example, uh, if you do an HTML page or like a React page, um, and you include image components, right, you sometimes see that your whole page is jumping around, right? So you go to a website and suddenly you see like this huge image like sliding everything down and that kind of thing.
[00:46:02] Michael Chan: Right, yeah.
[00:46:03] Tim Neutkens: Um, so AMP actually has this component called AMP image, which requires you to set width and height. Uh so basically the aspect ratio. Uh so it makes sure that you don't get into this pitfall of like everything jumping around. But then also it does like lazy loading of images only when uh they're in your viewport. Hmm. Um so it optimizes a lot of things. And then also we implemented it in a way that allows you to build uh basically Next.js applications that render to AMP. But then you can do, for example, either hybrid mode, which is doing uh a Next.js application and then serving AMP for uh search engines, uh like Google, for example. Uh so, it indexes a different page for mobile on your uh Google results.
[00:46:53] Michael Chan: Okay.
[00:46:53] Tim Neutkens: Um but also, you can do full AMP pages, which is what we do for nextjs.org, where you build your whole application or like a whole page because it's incremental incrementally adoptable. You can just change, for example, one page, like your homepage or your about page to AMP, and then render everything in AMP. So even the the desktop site, for example.
[00:47:18] Michael Chan: Crazy. That's incredible. That's incredible. I imagine that was uh that was quite an effort. It sounds like that's a very kind of strict— uh those are very strict guidelines.
[00:47:27] Tim Neutkens: Yeah. So uh we basically implement all the um the things around implementing AMP. So AMP has this boilerplate that you always have to include. Um, you have this optimizer for if you don't render inside of Google. Uh, you need to optimize it, or else you get a flash of, like, white, basically.
[00:47:45] Michael Chan: Okay.
[00:47:46] Tim Neutkens: So you get a flash of- it's not styled content, but it's just a white screen before the JavaScript loads. Uh, but we got rid of that using AMP Optimizer, which is included out of the box. Um, and then also, like, the developer experiences, everything that you expect from Next. So, for example, if you go to an AMP page in development and you make changes to components, it automatically refreshes the page and that kind of thing.
[00:48:10] Michael Chan: Crazy. Now, which version of Next do you need to be using to uh get these this AMP feature?
[00:48:17] Tim Neutkens: Uh the latest stable version, which is 8.1.
[00:48:20] Michael Chan: Okay.
[00:48:21] Tim Neutkens: And that has come out last week. So, it's already uh available for everyone.
[00:48:25] Michael Chan: Great, great. Awesome. Well, we have spent uh quite a bit of time talking about Next, and I really appreciate your uh your availability and taking the time to talk with us about all the all the stuff that you're doing and what's kind of coming down the pipe. Uh where can people find out more about Next? Where can they follow you and what you're doing?
[00:48:45] Tim Neutkens: Right. So, uh we have a website for Next.js, which is nextjs.org. Um but on the website, there's actually a learn section that is interactive. So, you I don't know if you went through it uh yourself, but—
[00:48:58] Michael Chan: Yeah.
[00:49:00] Tim Neutkens: It's basically-
[00:49:01] Michael Chan: It's great. It's terrific.
[00:49:02] Tim Neutkens: Yeah. So, if you go there, uh you can get started using Next.js, but it's very um practical. So, if you go there, you basically get like an introduction of what Next.js is. It goes through all the APIs, but it does so in a very playful manner. So, it asks you to clone uh code and then like run Next.js uh yourself. Make some changes. There's like quizzes inside of there also.
[00:49:29] Michael Chan: Yeah.
[00:49:29] Tim Neutkens: And it's a really nice way to get started using Next.js because you get to play around with the framework while learning about all the features that it has.
[00:49:39] Michael Chan: It's extremely well-considered and it's a very playful and fun way to get started with a with a library. I I really liked it.
[00:49:46] Tim Neutkens: Yeah, definitely. And we also, for example, just added in uh a lesson on using AMP, for example. If you want to get into that.
[00:49:53] Michael Chan: Awesome.
[00:49:54] Tim Neutkens: So it's still expanding out uh based on features that we introduce.
[00:49:58] Michael Chan: And where can people find you about the internet?
[00:50:01] Tim Neutkens: I'm on Twitter uh as @TimNoedkens, which is uh probably going to be in the description of your uh podcast because it's really hard to um just write down based on what you say. But yeah, yeah, I'm on Twitter, GitHub, anywhere basically uh under the same name. Yeah, that's basically it.
[00:50:20] Michael Chan: Awesome. So as we bring this to a close, I just wanted to ask, is there anything that we uh that you're excited about that we haven't covered that you want to touch on before we before we close it out?
[00:50:30] Tim Neutkens: So currently we're working on uh a new feature of the Next uh build API, which is what builds the production bundles for your application, which is that um currently if you do, for example, uh you have an application and you have Like say 50 pages or something, um and you run Next build 2 times, it will take probably like 30 seconds or more uh to build out all these pages. But we're currently working on a way to detect what actually changed in between those builds, uh basically through like diffing the uh components that you're uh changing. So if you import uh component in only one page and you change that one component, it will only rebuild that one page that you imported it in.
[00:51:22] Michael Chan: Okay.
[00:51:22] Tim Neutkens: Which means that if you, for example, rebuild this 50-page application, it would only take 1 second instead of like 30-plus seconds to build out all these pages. Um, and then you're basically in production immediately, right? So you run next build and um everything rebuilds, like only the things that you changed rebuild.
[00:51:45] Michael Chan: Nice. Well, we might have to get you back on to talk about uh some of the more like how it plays into the Zeit strategy and and all that, but um you know, for for another time.
[00:51:55] Tim Neutkens: Yeah.
[00:51:55] Michael Chan: Uh thank you so much, Tim, for joining us today. I really appreciate it.
[00:51:58] Tim Neutkens: Sounds good. Thank you very much.
[00:52:00] Michael Chan: Thanks so much for listening to React Podcast. I hope you'll give Next.js a try. I'm pretty sure you'll love it. It's one of my favorites. In fact, I am using it right now to build the next version of reactpodcast.com. Links and notes for this episode are available at reactpodcast.com/53. And thanks again to our sponsors, GitPrime and G2i. Visit g2i.co to find React and React Native talent fast. Don't waste your team's precious resource of time trying to find superstar React devs. Visit g2i.co and find vetted React talent specialized for your needs. Visit getprime.com/20patterns to get your free printed copy of 20 Patterns to Watch for in Your Engineering Team. It's beautifully illustrated and will 100% improve your team's communication and productivity. This episode of React Podcast was edited by Mikael Delport. It was produced by Mikael Delport and Sarah Jackson. You can find React Podcast on Speck, a network to help designers and developers level up. Visit spec.fm to find other shows that will take you further in your career. Help us out by reviewing this show on iTunes. Your reviews help the show grow and help us ensure great guests and awesome content week to week. To join the discussion, visit reactpodcast.com/discuss. Or follow us on Twitter @ReactPodcast. I'm @Chantastic. To stay out of the discussion but get updates, visit reactpodcast.com/news and sign up for emails. Thanks so much for giving us your attention. We'll be in your ears again next week.
chantastic.fm