Next.jshas a limitation on the project structure - all of the pages should be located in the pages directory. Mosaicremoves this limitation, thus allowing for a modular project structure. Each Mosaic module is able to register its own pages, which will be taken into account during the build time.
Preparations
In order to declare a Next.js page, some preparations are necessary. This will work only in Next.js powered by Mosaic, launched with package @tilework/mosaic-nextjs-scripts
First of all, an extension module must be created. You will implement your page there.
For the development purposes, it's recommended to have the under-development extension module installed into your project.
In order to have an additional page in a Mosaic-powered Next.js application, the first step is to register this page in order to let the Mosaic build system know about the page. It should be done in the following way.
Instead of the server you may use one of the values described above.
A template page, similar to the following, will be generated after the actions above. For different page types, different templates will be generated. This will cause additional namespaces to be available for plugins.
Add functionality and contents to it with plugins!
import renderEmptyPage from'@tilework/mosaic-nextjs-scripts/lib/empty-page';/** * ! This file is a "placeholder" / injection point generated, * ! so that the NextJS static router (and Babel plugin) would * ! recognize this file as a page. You can override this page * ! behaviour using plugin system! *//** @namespace Pages/pagename/Page */constPage= () => (process.env.NODE_ENV==='production'?null:renderEmptyPage({ ... }));/** @namespace Pages/pagename/getServerSideProps */constgetServerSideProps= () => ({ props: {} });export { getServerSideProps };exportdefault Page;
import renderEmptyPage from'@tilework/mosaic-nextjs-scripts/lib/empty-page';/** * ! This file is a "placeholder" / injection point generated, * ! so that the NextJS static router (and Babel plugin) would * ! recognize this file as a page. You can override this page * ! behaviour using plugin system! *//** @namespace Pages/pagename/Page */constPage= () => (process.env.NODE_ENV==='production'?null:renderEmptyPage({ ... }));exportdefault Page;
import renderEmptyPage from'@tilework/mosaic-nextjs-scripts/lib/empty-page';/** * ! This file is a "placeholder" / injection point generated, * ! so that the NextJS static router (and Babel plugin) would * ! recognize this file as a page. You can override this page * ! behaviour using plugin system! *//** @namespace Pages/pagename/Page */constPage= () => (process.env.NODE_ENV==='production'?null:renderEmptyPage({ ... }));/** @namespace Pages/pagename/getStaticProps */constgetStaticProps= () => ({ props: {} });export { getStaticProps };exportdefault Page;
Implement the page
The code example shown below explains, how to implement the page declared above. It uses our plugin system, if you are not yet familiar to it - get acknowledged in this guide.