I'm using NextJS and Fullcalendar.
I tried to use fullcalendar using dynamic import like in this example(for more info, this example solution comes from here).
It worked, But there was a problem. Almost every 1 out of 5 refreshing attempts ended in error Please import the top-level fullcalendar lib before attempting to import a plugin (like this, but versions are correct in my case)
After that, I found out that the modules option for next/dynamic has been deprecated. I thought it was a source of my problem (I'm not sure 100%, but at least it was deprecated and needed to be updated).
As docs says, newer way to handle dynamic imports is like this:
const DynamicComponent = dynamic(() =>
import('../components/hello').then((mod) => mod.Hello)
)
But because we need multiple imports I found this solution.
For now, It seems like everything should be working, but my code has no effect.
import dynamic from "next/dynamic";
import { useEffect, useState } from "react";
import "@fullcalendar/common/main.css"; // @fullcalendar/react imports @fullcalendar/common
import "@fullcalendar/daygrid/main.css"; // @fullcalendar/timegrid imports @fullcalendar/daygrid
import "@fullcalendar/timegrid/main.css"; // @fullcalendar/timegrid is a direct import
import "./Fullcalendar.module.scss";
let CalendarComponent;
const Calendar = dynamic(() =>
import("@fullcalendar/react").then((module) => module.Calendar)
);
const dayGridPlugin = dynamic(() =>
import("@fullcalendar/daygrid").then((module) => module.dayGridPlugin)
);
export default function FullCalendar(props) {
const [calendarLoaded, setCalendarLoaded] = useState(false);
useEffect(() => {
CalendarComponent = () => (
<Calendar
{...props}
plugins={[dayGridPlugin]}
initialView="dayGridMonth"
/>
);
setCalendarLoaded(true);
}, []);
let showCalendar = (props) => {
if (!calendarLoaded) return <div>Loading ...</div>;
return <CalendarComponent {...props} />;
};
return <div>{showCalendar(props)}</div>;
}
I also find another way to use next transpile modules.
But they say that "there is currently no way to transpile only parts of a package, it's all or nothing".
In fact, I have something in next.config.js. Editing this file into transpile modules is another mysterious adventure, full of problems.
What should I have to do?
Making FullCalendar work properly with Next.js requires some initial setup and a few changes to your initial code.
First, let's install
next-transpile-modulesto processfullcalendar's ES modules. Make sure to include all@fullcalendardependencies you use.Next, add a custom Babel configuration to ignore CSS imports used in
fullcalendar-babel-plugin-transform-require-ignoreplugin will need to be installed. This prevents theGlobal CSS cannot be imported from within node_moduleserror from Next.js.You'll have to include
fullcalendar's global CSS in_app.jsto compensate for this. Global CSS can only be imported from this file in Next.js.Finally, you can simplify and refactor your
FullCalendarcomponent. Here you don't have to dynamically import itsfullcalendardependencies, we'll dynamically import the component itself when using it.Then, dynamically import your custom component wherever it's used.
For reference, this is based on the official example from
fullcalendar, with some adaptations to make it work withnext-transiple-modulesv6.4.0.