Skip to content
Snippets Groups Projects
Commit 24e56a6a authored by sixertoy's avatar sixertoy
Browse files

:gear: ajout des routes directement dans le fichier server.js

- les routes ne sont plus importées dynamiquement
parent 478cbefe
Branches
Tags
No related merge requests found
/* eslint
indent: [2, 2],
semi: [2, "always"],
react/jsx-indent: [2, 2],
react/jsx-indent-props: [2, 2],
max-nested-callbacks: [2, { "max": 4 }],
react/jsx-closing-bracket-location: [2, {
"nonEmpty": false,
"selfClosing": false
}],
"jsx-a11y/anchor-is-valid": [2, {
"components": ["Link"],
"specialLink": ["hrefLeft", "hrefRight"]
}],
import/order: [2, {
newlines-between: "always",
groups: ["builtin", "external", "parent", "sibling", "index"]
}]
*/
export const renderApplicationPageFromServer = (
routeObject,
nextApplication,
) => (req, res) => {
const { page, query } = routeObject;
nextApplication.render(req, res, page, query(req));
};
export const generateServerDynamicRoutes = (
nextApplication,
expressServer,
dynamicRoutes,
) => {
// Génére les routes côté server ExpressJS
// Cela permet de récupérer par exemple un token dans l'URL
// Et d'afficher la popin de confirmation d'enregistrement
Object.keys(dynamicRoutes).forEach((routePath) => {
const routeObject = dynamicRoutes[routePath];
const callback = renderApplicationPageFromServer(
routeObject,
nextApplication,
);
expressServer.get(routePath, callback);
});
};
/* eslint
indent: [2, 2],
semi: [2, "always"],
react/jsx-indent: [2, 2],
react/jsx-indent-props: [2, 2]
max-nested-callbacks: [2, { "max": 4 }]
*/
import { generateServerDynamicRoutes } from "../generateServerDynamicRoutes";
const mockDynamicRoutes = {
"path/to/route": {
page: "/",
query: () => {},
},
};
const mockApp = { render: jest.fn() };
const mockServer = { get: jest.fn() };
describe("lib | generateServerDynamicRoutes", () => {
afterEach(() => {
mockServer.get.mockClear();
});
it("doit appeler la methode get du server", () => {
generateServerDynamicRoutes(mockApp, mockServer, mockDynamicRoutes);
expect(mockServer.get).toHaveBeenCalledTimes(1);
});
});
......@@ -25,7 +25,14 @@ const { assign, concat, flow } = require("lodash/fp");
// eslint-disable-next-line import/no-extraneous-dependencies
const { EnvironmentPlugin } = require("webpack");
const { internalStaticNextJSRoutes } = require("./routes");
const internalStaticNextJSRoutes = {
// Routes internes à l'application utilisées par next.config.js
// Ces routes ne peuvent pas comporter des parametres
"/en-savoir-plus": {
page: "/",
query: { showPopin: "en-savoir-plus" },
},
};
const nextConfig = {
exportPathMap: defaults => ({
......
/* eslint
indent: [2, 2],
semi: [2, "always"],
import/order: [2, {
newlines-between: "always",
groups: ["builtin", "external", "parent", "sibling", "index"]
}],
implicit-arrow-linebreak: [2, "below"]
*/
/* --------------------------------------------------------
//
//
// !!! A utiliser uniquement dans les cas:
// - de redirection sur une page particuliere
// - ou d'affichage de popin sur une page particuliere depuis l'URL
// Ex: la popin de confirmation d'enregistrement
//
//
-------------------------------------------------------- */
const externalDynamicExpressJSRoutes = {
// Routes externes à l'application utilisées par server.js
// Ces routes ont besoin d'un parametre dynamique pour fonctionner
"/connection/:token": {
page: "/",
query: req =>
({
token: req.params.token,
showPopin: "confirmation-connexion",
}),
},
};
const internalStaticNextJSRoutes = {
// Routes internes à l'application utilisées par next.config.js
// Ces routes ne peuvent pas comporter des parametres
"/en-savoir-plus": {
page: "/",
query: { showPopin: "en-savoir-plus" },
},
};
module.exports = { externalDynamicExpressJSRoutes, internalStaticNextJSRoutes };
......@@ -24,14 +24,49 @@ import express from "express";
import bodyParser from "body-parser";
import cookieParser from "cookie-parser";
import { externalDynamicExpressJSRoutes } from "./routes";
import { generateServerDynamicRoutes } from "./lib/generateServerDynamicRoutes";
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });
const handle = app.getRequestHandler();
const port = parseInt(process.env.PORT, 10) || 9001;
const renderApplicationPageFromServer = (routeObject, nextApplication) => (
req,
res,
) => {
const { page, query } = routeObject;
nextApplication.render(req, res, page, query(req));
};
const generateServerDynamicRoutes = (
nextApplication,
expressServer,
dynamicRoutes,
) => {
// Génére les routes côté server ExpressJS
// Cela permet de récupérer par exemple un token dans l'URL
// Et d'afficher la popin de confirmation d'enregistrement
Object.keys(dynamicRoutes).forEach((routePath) => {
const routeObject = dynamicRoutes[routePath];
const callback = renderApplicationPageFromServer(
routeObject,
nextApplication,
);
expressServer.get(routePath, callback);
});
};
const externalDynamicExpressJSRoutes = {
// Routes externes à l'application utilisées par server.js
// Ces routes ont besoin d'un parametre dynamique pour fonctionner
"/connection/:token": {
page: "/",
query: req => ({
token: req.params.token,
showPopin: "confirmation-connexion",
}),
},
};
async function start() {
await app.prepare();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment