What’s the difference between exposing environment variables in nextjs through the next.config.js vs with the NEXT_PUBLIC prefix?

NEXT_PUBLIC is a new feature added. Before, in order to set up environment variables, we had to set up both server and client, separately. Environment variables that are placed in the .env file would be available only on the server-side, if you want to make your env variables available on the client-side you had to … Read more

Angular 9 SSR 404 Not Found Page with Status code

Create Server Side Response Service @Injectable({ providedIn: ‘root’ }) export class ServerSideResponseService { private response: Response constructor(@Optional() @Inject(RESPONSE) response: any) { this.response = response } setNotFound(message=”not found”): this { if (this.response) { this.response.statusCode = 404 this.response.statusMessage = message } return this } } and use it in Not Found Component @Component({ selector: ‘app-not-found’, templateUrl: ‘./not-found.component.html’, … Read more

How to use cookie inside `getServerSideProps` method in Next.js?

You can get the cookies from the req.headers inside getServerSideProps: export async function getServerSideProps(context) { const cookies = context.req.headers.cookie; return { props: {}, }; } You could then use the cookie npm package to parse them: import * as cookie from ‘cookie’ export async function getServerSideProps(context) { const parsedCookies = cookie.parse(context.req.headers.cookie); return { props: {} … Read more

Module not found: Can’t resolve ‘fs’ in Next.js application

If you use fs, be sure it’s only within getInitialProps or getServerSideProps. (anything includes server-side rendering). You may also need to create a next.config.js file with the following content to get the client bundle to build: For webpack4 module.exports = { webpack: (config, { isServer }) => { // Fixes npm packages that depend on … Read more

Error: How to serialize data from getStaticProps : Next.js

Add JSON.stringify when calling an asynchronous function that returns an object. Try modifying your getStaticProps function like this. export async function getStaticProps() { const profiles = await getAllBusinessProfiles(); const allProfiles = JSON.stringify(profiles) return { props: { allProfiles } }; } The JSON.stringify() method converts a JavaScript object or value to a JSON string, optionally replacing … Read more

Create a HOC (higher order component) for authentication in Next.js

You should separate and extract your authentication logic from getServerSideProps into a re-usable higher-order function. For instance, you could have the following function that would accept another function (your getServerSideProps), and would redirect to your login page if the userToken isn’t set. export function requireAuthentication(gssp) { return async (context) => { const { req, res … Read more

How to make a dynamic import in Nuxt?

Since the error was thrown during the import statement, I’d recommended using dynamic imports as explained in my other answer here. async mounted() { if (process.client) { const Ace = await import(‘ace-builds/src-noconflict/ace’) Ace.edit… } }, From the official documentation: https://nuxtjs.org/docs/2.x/internals-glossary/context EDIT: I’m not sure about Ace and it’s maybe a drastic change but you may … Read more