mosaic
  • Overview
  • Integration
    • Next.js
    • Create React App
    • Webpack
  • Getting started
    • Extensions
    • Themes
  • Tutorials
    • Mosaic + React
    • Mosaic + Webpack
  • Install an extension
    • Install a local extension
    • Install with a package manager
    • Enable or disable an extension
  • Develop an extension
    • Anatomy of an extension
    • Namespaces
    • Runtime plugins
    • Build configuration plugins
  • CRA features
    • CRACO plugins
  • Next.js features
    • Styles
    • Pages
    • Common props
  • Architecture examples
    • Router
  • Themes
    • Parent theme system
    • File shadowing
  • Experimental
    • Module preferences
    • File provisioning
  • in-depth
    • How does it work?
Powered by GitBook
On this page
  • Motivation
  • Preparations
  • Page type
  • Register a page
  • See the magic
  • Implement the page

Was this helpful?

  1. Next.js features

Pages

Declare a Next.js page in your Mosaic module

PreviousStylesNextCommon props

Last updated 4 years ago

Was this helpful?

Motivation

Next.js has a limitation on the project structure - all of the pages should be located in the pages directory. Mosaic removes 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 . You will implement your page there.

For the development purposes, it's recommended to have the under-development extension module into your project.

Page type

The following page types are available:

  • : server

  • : static-no-data

  • : static-with-data

Register a page

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.

@example/page-module/package.json
{
    "mosaic": {
        "nextPages": {
            "pagename": "server"
        }
    }
}

See the magic

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 */
const Page = () => (
    process.env.NODE_ENV === 'production'
        ? null
        : renderEmptyPage({ ... })
);

/** @namespace Pages/pagename/getServerSideProps */
const getServerSideProps = () => ({ props: {} });

export { getServerSideProps };
export default 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 */
const Page = () => (
    process.env.NODE_ENV === 'production'
        ? null
        : renderEmptyPage({ ... })
);

export default 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 */
const Page = () => (
    process.env.NODE_ENV === 'production'
        ? null
        : renderEmptyPage({ ... })
);

/** @namespace Pages/pagename/getStaticProps */
const getStaticProps = () => ({ props: {} });

export { getStaticProps };
export default Page;

Implement the page

const serverSideProps = async ([context]) => ({ 
    props: { 
        sample: 'example for a server-side prop' 
    }
});

const page = ([{ sample }]) => <Typography content={sample} />;

export default {
    'Pages/pagename/getServerSideProps': {
        function: serverSideProps
    },
    'Pages/pagename/Page': {
        function: page
    }
}

Instead of the server you may use one of the 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 to be available for .

Add functionality and contents to it with !

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 .

created
installed
SSR
Static Generation without data
Static Generation with data
namespaces
plugins
plugins
this guide
values