question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Common Next.js issues that could use integrated ESLint rules

See original GitHub issue

Describe the feature you’d like to request

These are common issues developers run into with Next.js. Some are mentioned in the docs, but ideally you don’t have to go check the docs.

Describe the solution you’d like

Instead, ESLint can provide compile-time feedback and suggestions.

  • Trying to call an API route from inside getStaticProps / getServerSideProps leading to next build failing.

  • Trying to use getStaticProps inside of _app.js, assuming it works like the other pages, but it doesn’t.

  • Forgetting to await an async function inside an API route, works fine locally but does out of band work which will cause issues if there’s multiple requests.

    • Code Example

      // Found on stackoverflow
      export default function handler(req, res) {
          const { method } = req;
          
          switch (method) {
              case 'GET':
      	   // but this is making a fetch and has no await....
                  getRestaurants();
                  break;
              default:
                  res.status(405).end(`Method ${req.method} Not Allowed`);
          }
      
          function getRestaurants() {
              const restaurants = data;
              res.status(200).json(restaurants);
          }
      }
      
  • Using document or other browser APIs outside of useEffect

  • File name casing (e.g. myFile.js vs MyFile.js). This could potentially be solved with linting if import statement diffs from the actual file path? Not sure.

Describe alternatives you’ve considered

N/A

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:18
  • Comments:18 (13 by maintainers)

github_iconTop GitHub Comments

4reactions
nguyen-michaelcommented, Oct 5, 2021

@leerob

Thoughts on a rule that lints if next/image is using layout= fill or responsive but isn’t setting explicit sizes prop?

Docs state:

A string mapping media queries to device sizes. Defaults to 100vw. We recommend setting sizes when using layout=“responsive” or layout=“fill” and your image will not be the same width as the viewport.

The rule could be opted into as a warning and reminder to set proper sizes or risk loading images that are too large if they’re not going to be 100vw.

I’ve got a working implementation of this rule in a custom ESLint plugin stored on a private repo but would happily put it up for review if you think it would be handy!

3reactions
LetItRockcommented, Aug 5, 2021

hi all! @leerob I would like to try to implement this rule: Another one: throw an error if using fs or other node packages on the client side.

Just to clarify, the rule should not allow using node built-in modules inside the react components, but will allow inside the other files like ex. utils, API routes and inside the data fetching functionsgetStaticProps, getStaticPaths, getServerSideProps is that correct?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Basic Features: ESLint - Next.js
Next.js provides an integrated ESLint experience by default. These conformance rules help you use Next.js in the optimal way.
Read more >
Troubleshooting a Next.js app with ESLint - LogRocket Blog
js . Now, let's look at how we can apply some strict rules to maintain a good code structure. This is the ESLint...
Read more >
How to configure EsLint in Nextjs? | by Rajdeep singh - Medium
How to configure EsLint in Nextjs? What is EsLint? EsLint is a tool that helps find the problem and error in code within...
Read more >
How to Automate Code Linting in Next.js with ESLint & Husky ...
How can we take advantage of linting in our Next.js apps and even ... on a Next.js application; Step 2: Customizing Next.js ESLint...
Read more >
Configure ESLint for Next.js - Devin Shoemaker
Generate ESLint Config · Select To check syntax and find problems · Select JavaScript modules as the type of module your project uses...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found