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

Eliminate Deprecated DNS Module Import #371

Closed
wants to merge 1 commit into from

Conversation

ram-you
Copy link
Contributor

@ram-you ram-you commented Nov 28, 2023

This PR addresses a deprecated DNS module import, following Vite's documentation guidance. Vite discourages the use of Node.js modules in the browser due to potential bundle size increase. The import block has been removed, aligning the codebase with recommended practices for more efficient and browser-compatible builds. The adjustment reflects Vite's emphasis on minimizing the inclusion of Node.js-specific modules in client-side code.

I would like to extend my sincere apologies for any inconvenience caused by the recent pull request https://github.com/nuxt-modules/strapi/pull/370.
After careful reconsideration, I've recognized an error in my assessment, and the changes made in the previous pull request were not aligned with the best interests of the project. This new pull request aims to definitely remove the DNS module import, ensuring that the application's functionality is maintained as intended. I appreciate your understanding, and I apologize for any confusion caused.

Types of changes

  • Bug fix (a non-breaking change which fixes an issue)
  • New feature (a non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Description

Vite recommend avoiding Node.js modules for browser code to reduce the bundle size

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes (if not applicable, please state why)

This PR addresses a deprecated DNS module import, following Vite's documentation guidance. Vite discourages the use of Node.js modules in the browser due to potential bundle size increase. The import block has been removed, aligning the codebase with recommended practices for more efficient and browser-compatible builds. The adjustment reflects Vite's emphasis on minimizing the inclusion of Node.js-specific modules in client-side code.
I would like to extend my sincere apologies for any inconvenience caused by the recent pull request. After careful reconsideration, I've recognized an error in my assessment, and the changes made in the previous pull request were not aligned with the best interests of the project. This new pull request aims to definitely remove the DNS module import, ensuring that the application's functionality is maintained as intended. I appreciate your understanding, and I apologize for any confusion caused.
Copy link

netlify bot commented Nov 28, 2023

👷 Deploy request for nuxt-strapi-module pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit e95b2de

@benjamincanac
Copy link
Member

Why would you remove this? I've just added this to fix the cookie being removed on localhost with Node 18 as you can read in the comment above this code.

@ram-you
Copy link
Contributor Author

ram-you commented Nov 28, 2023

Why would you remove this? I've just added this to fix the cookie being removed on localhost with Node 18 as you can read in the comment above this code.

Hi @benjamincanac, thank you for your comment. I removed the code because Vite was still generating a warning despite the fix. The warning message indicates that the "dns" module has been externalized for browser compatibility. You can find more details about this warning here.
I'll explore alternative solutions to resolve the cookie issue.
I appreciate your understanding.

@ram-you
Copy link
Contributor Author

ram-you commented Nov 29, 2023

Hi @benjamincanac
To address the Vite "Warning" issue while retaining your solution for "the cookie being removed on localhost with Node 18", I have created a Nuxt Server Plugin in my project named "/plugins/dns_ipv4first.server.ts" with the following code:

export default defineNuxtPlugin({
  name: 'dns_ipv4first',
  parallel: true,
  async setup (nuxtApp) {
    // Fixes `ECONNREFUSED` on Node 18: https://github.com/node-fetch/node-fetch/issues/1624#issuecomment-1407717012
    // Import dns only if running in a server environment during development
    if (process.dev) {
      try {
        const dns = await import("dns");
        await dns.setDefaultResultOrder("ipv4first");
      } catch (error) {
        console.error("Error importing dns module:", error);
      }
    }
  }
});

And the issue is resolved. What do you think of my approach?

Copy link
Member

It works for me! Do you feel like updating your PR with this? 😊

Why import.meta.env.DEV instead of process.dev?

If you make this change I would create a plugins dir inside runtime and rename strapi.plugin -> strapi. You could now add this dns.server.ts inside this plugins dir.

@ram-you
Copy link
Contributor Author

ram-you commented Nov 29, 2023

If you make this change I would create a plugins dir inside runtime and rename strapi.plugin -> strapi. You could now add this dns.server.ts inside this plugins dir.

So we need an other PR to create a new file : "/runtime/plugins/dns.server.ts":

export default defineNuxtPlugin({
  name: 'dns_ipv4first',
  parallel: true,
  async setup (nuxtApp) {
    // Fixes `ECONNREFUSED` on Node 18: https://github.com/node-fetch/node-fetch/issues/1624#issuecomment-1407717012
    // Import dns only if running in a server environment during development
    if (process.dev) {
      try {
        const dns = await import("dns");
        await dns.setDefaultResultOrder("ipv4first");
      } catch (error) {
        console.error("Error importing dns module:", error);
      }
    }
  }
});

Copy link
Member

I can push directly if you prefer, just let me know!

@ram-you
Copy link
Contributor Author

ram-you commented Nov 29, 2023

I can push directly if you prefer, just let me know!

Yes please

@benjamincanac
Copy link
Member

Closing in favor of 13aa063

@benjamincanac
Copy link
Member

Thanks @ram-you!

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

Successfully merging this pull request may close these issues.

2 participants