There is a very large hard-coded array in my Next js application and I'm importing it in a few other places to use. Because of that, it has been included in the Next js main bundle. Since it is a very large file, I would like to remove it from the main bundle. Even though for components we can use next/dynamic to do this, for an array I couldn't find any method to resolve this. Could anyone help me with this one, please? Any kind of help is greatly appreciated.
Asked
Active
Viewed 78 times
1
-
Can you create an API route with it and call it when needed? – Anthony Ma Sep 17 '22 at 00:54
-
@AnthonyMa yeah! since it's an array, I could return it through an API. – Gayan Charith Sep 17 '22 at 02:06
-
Awesome, I'd appreciate it if you mark my post as the answer. – Anthony Ma Sep 17 '22 at 14:01
-
You can also just dynamically import the array (or whatever data structure) with [dynamic `import()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import). – juliomalves Sep 26 '22 at 17:34
1 Answers
1
You can create an API route and call it when needed, that's the benefit of Next JS, very easy to do that.

Anthony Ma
- 624
- 5
- 19