Coder Social home page Coder Social logo

Comments (1)

jens-duttke avatar jens-duttke commented on June 1, 2024 18

This works for me:

  1. Create a file scripts/why-did-you-render/index.js:
const path = require('path');

/** @typedef {Parameters<import('next').NextConfig['webpack']>[1]} WebpackConfigContext */

const injectionSource = path.join(__dirname, 'injection.ts');

/**
 * @param {import('webpack').Configuration} config
 * @param {WebpackConfigContext} context
 */
module.exports = (config, context) => {
	if (context.dev && !context.isServer) {
		const originalEntry = config.entry;

		config.entry = async () => {
			const entries = await originalEntry();

			if (entries['main-app'] && !entries['main-app'].includes(injectionSource)) {
				entries['main-app'].unshift(injectionSource);
			}

			return entries;
		};
	}
};

Here, instead of entries['main.js'] (which is used for the pages folder), entries['main-app'] is used (which is required for the app folder).

  1. Create a file scripts/why-did-you-render/injection.ts (you could also call it injection.js if you don't use TypeScript, and change it in the code above).
import React from 'react';

import whyDidYouRender from '@welldone-software/why-did-you-render';

// eslint-disable-next-line no-console -- Show information that `whyDidYouRender` has been applied to the website.
console.debug('Applying whyDidYouRender, to help you locate unnecessary re-renders during development. See https://github.com/welldone-software/why-did-you-render');

// See https://github.com/welldone-software/why-did-you-render#options
whyDidYouRender(React, {
	trackAllPureComponents: true,
	trackHooks: true,
	logOwnerReasons: true,
	collapseGroups: true,
	include: [/./],

	// This is for testing, remove it, if you don't want to log on different values
	logOnDifferentValues: true
});

In your next.config.js add:

const injectWhyDidYouRender = require('./scripts/why-did-you-render');

/** @type {import('next').NextConfig} */
module.exports = {
	...
	webpack: (config, context) => {
		injectWhyDidYouRender(config, context)

		return config;
	}
};

The include: [/./] in the whyDidYouRender options is a workaround for a bug, which seems to be in

return !!(
Component.whyDidYouRender || (
wdyrStore.options.trackAllPureComponents && (
(Component && Component.prototype instanceof wdyrStore.React.PureComponent) ||
isMemoComponent(Component)
)
) ||
shouldInclude(displayName)
);

from why-did-you-render.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.