Structuring a Next.js SPA for MVVM
Given that Next.js is a React-based framework, it lends itself very well to the MVVM design pattern. In this section, we will explain how to structure a Next.js single-page application (SPA) to conform the MVVM architecture patterns.
A default Next.js application has the following structure:
-- mvvm-demo
-- node_modules
-- app
-- public
We recommend modifying the default Next.js project structure when making use of the MVVM design pattern as shown below:
-- mvvm-demo
-- node_modules
-- components
-- models
-- app
-- viewmodels
-- api
-- utils
-- config
-- public
-- assets
-- styles
In this updated project structure:
- The
modelsdirectory contains the Models used in your project. - The
appdirectory and its subdirectories contain the Views. - The
viewmodelsdirectory contains the ViewModel files associated with each View. - The
componentsdirectory contains customized or reusable React components. - The
utilsdirectory contains common utility functions (e.g., handler functions for your custom components) - The
configdirectory contains constant data such as database details or the configuration settings needed for single sign-on, theme configuration, etc. - The
stylesdirectory contains the CSS styles used in your application. We recommend moving the CSS style sheets from theappdirectory to this newstylesdirectory. - The
apidirectory contains the API routes used to fetch, add, delete, or update data in your application. - The
public/assetsdirectory contains the assets used in your application including the favicon (that should be moved from theappdirectory) and any other images.