Skip to content
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

nextjs 'server-only' import issue during compile #30789

Open
Maus3rVonDutch opened this issue Dec 19, 2024 · 1 comment
Open

nextjs 'server-only' import issue during compile #30789

Maus3rVonDutch opened this issue Dec 19, 2024 · 1 comment

Comments

@Maus3rVonDutch
Copy link

Maus3rVonDutch commented Dec 19, 2024

Current behavior

Hi, I encouter an iusse when trying to run Cypress with a NextJS application that has a import 'server-only' (see https://nextjs.org/docs/app/building-your-application/rendering/composition-patterns#keeping-server-only-code-out-of-the-client-environment)
Does anyone have some insight how to resolve this? I am using NX btw.

I've tried to modify the webpack process, using @cypress/webpack-preprocessor and NormalModuleReplacementPlugin, but that results in other compiler issues. I don't now what options/which loaders to add to resolve the errors.

THis is the error I am seeing'

image

I've tried to do something like this, but like mentioned before, than I got other errors, like *.ts(x) files not recognized/transpiled (also tried to add a loader for that....)

module.exports = (on: Cypress.PluginEvents, config: Cypress.PluginConfigOptions) => {
    const options = webpackPreprocessor.defaultOptions;

  if (!!options?.webpackOptions) {
    console.log('Custom webpack pre-processing (mocking modules)');

    const serverOnlyMockPath = path.resolve('../../testing/__mocks__/empty.js');

    options.webpackOptions.plugins = options.webpackOptions.plugins ?? [];
    options.webpackOptions.plugins.push(
      new NormalModuleReplacementPlugin(/server-only/, (resource) => {
        if (resource.contextInfo.issuer?.includes('node_modules')) {
          return;
        }

        resource.request = resource.request.replace(/server-only/, serverOnlyMockPath);
        console.log(
          `TODO (meh): remove server-only mocking when possible. Issuer: [${resource.contextInfo.issuer}]`,
        );
      }),
    );
  }

  on('file:preprocessor', webpackPreprocessor(options));

Desired behavior

No response

Test code to reproduce

Currently not able to provide a test project, will try to provide this later

Cypress Version

13.13.0

NX/NextJS

NX 20.1.4
NextJS 14.2.3

Node version

v20.9.0

Operating System

windows 11

Debug Logs

No response

Other

No response

@jennifer-shehane
Copy link
Member

Try to update to the latest Cypress version. Also, check out our community chat, it can be helpful for debugging or answering questions on how to use Cypress.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants