{"id":13423790,"url":"https://github.com/revskill10/next-routes-middleware","last_synced_at":"2025-07-30T16:11:51.255Z","repository":{"id":33608674,"uuid":"159901591","full_name":"revskill10/next-routes-middleware","owner":"revskill10","description":"Universal Lambda Routing","archived":false,"fork":false,"pushed_at":"2022-03-07T09:59:47.000Z","size":108,"stargazers_count":40,"open_issues_count":11,"forks_count":9,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-24T16:01:30.675Z","etag":null,"topics":["expressjs","nextjs"],"latest_commit_sha":null,"homepage":"https://next-routes-middleware-4ari798pi.now.sh/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/revskill10.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-12-01T02:34:05.000Z","updated_at":"2023-06-22T17:59:14.000Z","dependencies_parsed_at":"2022-08-07T22:15:35.202Z","dependency_job_id":null,"html_url":"https://github.com/revskill10/next-routes-middleware","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/revskill10%2Fnext-routes-middleware","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/revskill10%2Fnext-routes-middleware/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/revskill10%2Fnext-routes-middleware/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/revskill10%2Fnext-routes-middleware/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/revskill10","download_url":"https://codeload.github.com/revskill10/next-routes-middleware/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248270466,"owners_count":21075794,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["expressjs","nextjs"],"created_at":"2024-07-31T00:00:42.547Z","updated_at":"2025-04-10T18:20:36.367Z","avatar_url":"https://github.com/revskill10.png","language":"JavaScript","funding_links":[],"categories":["Extensions","JavaScript"],"sub_categories":[],"readme":"## Next Routes Middleware\n\n[![npm version](https://d25lcipzij17d.cloudfront.net/badge.svg?id=js\u0026type=6\u0026v=3.4.0\u0026x2=0)](https://www.npmjs.com/package/next-routes-middleware)\n\nUniversal Lambda Routing for Next.JS application\n\n## Roadmap for 4.0.0\n\n1. [Integrate with existing `now.json` in `now.config.js` for better integration with existing ecosystem](https://github.com/revskill10/next-routes-middleware/issues/13)\n2. [Provide @now/node to replace @now/next (Next V8 provides a serverless lambda out of the box)](https://github.com/revskill10/next-routes-middleware/issues/10)\n\n## Demo\n\n[Demo](https://next-routes-middleware-4ari798pi.now.sh/)\n\n## Installation\n\n```\nnpm i --save next-routes-middleware\n```\n\n## Features\n\n- Replicate `next.js` for Now V2 config to develop locally.\n- Support named regular expressions used in `routes`.\n- Allow custom routes handling with all Express.js http methods.\n- Client-side routing\n- Compiling to Now V2 compatible JSON config\n\n## Usage\n\nStep 1: Create your own `now.config.js`:\n\n```js\nconst buildTypes = [\n  \"@now/next\",\n  \"@now/static\"\n]\nconst builds = [\n  {\n    src: \"next.config.js\", use: buildTypes[0]\n  },\n  {\n    src: \"static\", use: buildTypes[1]\n  }\n]\n\nconst nextRoutes = ['/about'].map(function(item, index) {\n  return {\n    src: item,\n    dest: item,\n    build: buildTypes[0],\n  }\n})\n\nconst deployConfig = {\n  version: 2,\n  name: \"next-routes-middleware\",\n  public: true,\n  alias: \"next-routes-middleware\"\n}\n\n\nconst patterns = {\n  first: \"(?\u003cfirst\u003e.*)\",\n  uuid: \"(?\u003cuuid\u003e[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}){1}\",\n  slug:\"(?\u003cslug\u003e[^/]*)\",\n  year:\"(?\u003cyear\u003e[0-9]{4})\",\n  month:\"(?\u003cmonth\u003e[0-9]{2})\",\n  day:\"(?\u003cday\u003e[0-9]{2})\"\n}\n\nconst routes = [\n  {\n    src: \"/favicon.ico\",\n    dest: \"static/favicon.ico\",\n    build: buildTypes[0],\n  },\n  ...nextRoutes,\n  { \n    src: `/w/${patterns.first}`, \n    dest: \"/work?slug=${first}\",\n    build: buildTypes[0],\n  },\n  { \n    src: `/resource/${patterns.uuid}`, \n    dest: \"/complex?id=${uuid}\",\n    build: buildTypes[0],\n  },\n  { \n    src: `/t/${patterns.slug}/${patterns.year}-${patterns.month}-${patterns.day}`, \n    dest: \"/more_complex?day=${day}\u0026month=${month}\u0026year=${year}\u0026slug=${slug}\" ,\n    build: buildTypes[0],\n  },\n  { src: \"/\", dest: \"/index\", build: buildTypes[0], }\n]\n\n\nmodule.exports = {\n  ...deployConfig,\n  patterns,\n  builds,\n  routes,\n}\n\n```\n\n*Note*: You can skip step 2 by using [now-dev-cli](https://github.com/revskill10/now-dev-cli), which will run this custom server for you.\n\nStep 2: Using `next-routes-middleware` in your custom `server.js`\n\n```js\nconst express = require('express')\nconst next = require('next');\nconst dev = process.env.NODE_ENV !== 'production';\nconst app = next({ dev });\nconst routesMiddleware = require('../../index')\nconst port = parseInt(process.env.PORT, 10) || 3000\nconst config = require('./now.config.js')\napp.prepare().then(() =\u003e {\n  const server = express()\n  const prefix = \"\"\n  routesMiddleware({server, app, config, prefix})\n  server.listen(port, (err) =\u003e {\n    if (err) throw err\n    console.log(`\u003e Ready on http://localhost:${port}`)\n  })\n})\n\n\n```\n\nStep 3: Using in `pages/more_complex.js`\n\n```js\nimport {withRouter} from 'next/router'\nimport Nav from '../components/nav'\nconst Page = ({router}) =\u003e {\n  const {\n    query\n  } = router\n\n  const {day, month, year, slug} = query\n  return (\n    \u003c\u003e\n    \u003cNav /\u003e\n    \u003cdiv\u003eSlug is {slug}\u003c/div\u003e\n    \u003cdiv\u003eYou requested {year}-{month}-{day}\u003c/div\u003e\n    \u003c/\u003e\n  )\n}\n\nexport default withRouter(Page)\n```\n\n## Customization and Overriding Routes\n\nSuppose you want to do something else beyond rendering, you can provide custom routes handler like this\n\n`customRoutes.js`\n```js\n\nfunction customRoutes(nextRoutes) {\n  return {\n    '/service-worker.js': function({app, req, res, join, dev}) {\n      const filePath = dev ? join(__dirname, '../static', 'service-worker.dev.js'): join(__dirname, '../static', 'service-worker.js')\n      app.serveStatic(req, res, filePath)\n    },\n    '/favicon.ico': function({app, req, res, join}) {\n      const filePath = join(__dirname, '../static', 'favicon.ico')\n      app.serveStatic(req, res, filePath)\n    },\n    '/static/wasm/*': function({app, req, res, next, handle, pathname}) {\n      res.setHeader('Content-Type', 'application/wasm')\n      handle(req, res, parsedUrl)\n    },    \n    '/': function({app, req, res, isMobile, query}) {\n      if (!isMobile) {\n        app.render(req, res, '/index', {phone: false, ...query })\n      } else {\n        app.render(req, res, '/index', {phone: true, ...query})\n      }\n    },    \n    ...nextRoutes,\n    '*': function({handle, req, res, parsedUrl}) {\n      handle(req, res, parsedUrl)\n    },\n  }\n}\n\nmodule.exports = customRoutes\n```\n\nThen use in your custom `server.js`\n\n```js\nconst customRoutes = require('./customRoutes')\nroutesMiddleware({server, app}, customRoutes)\n```\n\n## Compiled now config file\n\nThis is the compiled Now config file\n\n```json\n{\n  \"version\": 2,\n  \"name\": \"next-routes-middleware\",\n  \"public\": true,\n  \"alias\": \"next-routes-middleware\",\n  \"patterns\": {\n    \"first\": \"(?\u003cfirst\u003e.*)\",\n    \"uuid\": \"(?\u003cuuid\u003e[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}){1}\",\n    \"slug\": \"(?\u003cslug\u003e[^/]*)\",\n    \"year\": \"(?\u003cyear\u003e[0-9]{4})\",\n    \"month\": \"(?\u003cmonth\u003e[0-9]{2})\",\n    \"day\": \"(?\u003cday\u003e[0-9]{2})\"\n  },\n  \"builds\": [\n    {\n      \"src\": \"next.config.js\",\n      \"use\": \"@now/next\"\n    },\n    {\n      \"src\": \"static\",\n      \"use\": \"@now/static\"\n    }\n  ],\n  \"routes\": [\n    {\n      \"src\": \"/favicon.ico\",\n      \"dest\": \"static/favicon.ico\"\n    },\n    {\n      \"src\": \"/about\",\n      \"dest\": \"/about\"\n    },\n    {\n      \"src\": \"/w/(?\u003cfirst\u003e.*)\",\n      \"dest\": \"/work?slug=$first\"\n    },\n    {\n      \"src\": \"/resource/(?\u003cuuid\u003e[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}){1}\",\n      \"dest\": \"/complex?id=$uuid\"\n    },\n    {\n      \"src\": \"/t/(?\u003cslug\u003e[^/]*)/(?\u003cyear\u003e[0-9]{4})-(?\u003cmonth\u003e[0-9]{2})-(?\u003cday\u003e[0-9]{2})\",\n      \"dest\": \"/more_complex?day=$day\u0026month=$month\u0026year=$year\u0026slug=$slug\"\n    },\n    {\n      \"src\": \"/\",\n      \"dest\": \"/index\"\n    }\n  ]\n}\n```\n\n## Usage with next/link and styled-components for client-side routing\n\n`styled-link.js`\n```js\nimport Link from 'next/link'\nimport styled from 'styled-components'\nimport mkLink from 'next-routes-middleware/get-client-link'\nimport config from '../now.config.js'\nconst getClientLink = mkLink(config)\n\nconst NextLink = ({href, className, children, ...rest}) =\u003e {\n  const as = getClientLink(href)\n  return (\n    \u003cLink href={as} as={href} {...rest}\u003e\n      \u003ca className={className} href={as}\u003e{children}\u003c/a\u003e\n    \u003c/Link\u003e\n  )\n}\n\nconst StyledLink = styled(NextLink)`\n  color: #067df7;\n  text-decoration: none;\n  font-size: 13px;\n`\n\nexport default StyledLink\n```\n\n## Usage on client-side\n\n```js\nimport StyledLink from './styled-link'\n\u003cLi\u003e\n  \u003cStyledLink href='/w/test' passHref\u003eWork\u003c/StyledLink\u003e\n\u003c/Li\u003e\n\u003cLi\u003e\n  \u003cStyledLink prefetch href=\"/resource/202eb9d7-feb3-407c-922e-e749159cb3ec\" passHref\u003eResource\u003c/StyledLink\u003e\n\u003c/Li\u003e\n\u003cLi\u003e\n  \u003cStyledLink prefetch href=\"/t/hello-world/1999-12-31\" passHref\u003eMore resource\u003c/StyledLink\u003e\n\u003c/Li\u003e\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frevskill10%2Fnext-routes-middleware","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frevskill10%2Fnext-routes-middleware","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frevskill10%2Fnext-routes-middleware/lists"}