app-config icon indicating copy to clipboard operation
app-config copied to clipboard

Usage in NextJS Apps

Open mikelfcosta opened this issue 3 years ago • 5 comments

Hey!

I've used this library really well in SPA apps and express servers, and it worked perfectly with the schema validation and environment configs.

While trying to use it in a NextJS app, I ran into the following issues:

  • I could not use the NodeJS apis. Although I can import, run and read the configs successfully, it does not run on the Edge runtime that has no access to NodeJS native APIs (throws errors since some of those libs are not found).
  • I could not use the Webpack Plugin since it fails as there is no HtmlWebpack Plugin

Has anyone had success using this lib with a NextJS app, or if there are any plans for a plugin to support it?

mikelfcosta avatar Sep 16 '22 19:09 mikelfcosta

Hey thanks! I don't know very much about the NextJS build pipeline. But it sure sounds like a plugin we could build, similar (if not using) to the webpack plugin. You're right that today, the webpack plugin does only work if using the HTML plugin, something that most people do today.

You might be able to use the webpack plugin with headerInjection: false though. I think that's the only option that requires the HTML plugin.

joelgallant avatar Sep 18 '22 18:09 joelgallant

Looking at it, headerInjection: false doesn't quite solve the issue. While it may be the only config option using the plugin, the index file still tries to import from the library. Given that it's a peer dependency of @app-config/webpack it might not be best to assume the package exists in the index file.

Checking to see if the module exists first using require.resolve("html-webpack-plugin") might be a solution.

fstylermiller avatar Feb 10 '23 22:02 fstylermiller

the index file still tries to import from the library

Oh! The import there makes sense, I didn't notice that. So a lazy load instead would likely work.

joelgallant avatar Feb 11 '23 17:02 joelgallant

I'm looking to create a PR to lazy load the module! It doesn't seem that I have access to push to a new branch.

fstylermiller avatar Feb 24 '23 00:02 fstylermiller

I was able to create a PR for lazy loading: https://github.com/launchcodedev/app-config/pull/218

fstylermiller avatar Feb 24 '23 17:02 fstylermiller