├── .github └── ISSUE_TEMPLATE │ ├── bug_report.md │ └── feature_request.md ├── .gitignore ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── examples ├── .nowignore ├── api │ ├── .nowignore │ ├── index.js │ ├── now.json │ └── package.json ├── basic │ ├── .babelrc.js │ ├── .gitignore │ ├── README.md │ ├── components │ │ ├── head.js │ │ ├── language-switcher.js │ │ ├── layout.js │ │ ├── nav.js │ │ └── styles.js │ ├── env-config.js │ ├── lang │ │ ├── en.json │ │ └── fr.json │ ├── lib │ │ ├── intl-context.js │ │ ├── util.js │ │ └── with-intl.js │ ├── next.config.js │ ├── now.compiled.json │ ├── now.config.js │ ├── now.json │ ├── package.json │ ├── pages │ │ ├── _app.js │ │ ├── _document.js │ │ ├── about.js │ │ ├── complex.js │ │ ├── index.js │ │ ├── more_complex.js │ │ └── work.js │ ├── scripts │ │ └── default-lang.js │ ├── server.js │ ├── static │ │ └── favicon.ico │ └── yarn.lock ├── edu │ ├── .gitignore │ ├── README.md │ ├── components │ │ ├── head.js │ │ └── nav.js │ ├── next.config.js │ ├── package.json │ ├── pages │ │ └── index.js │ ├── static │ │ └── favicon.ico │ └── yarn.lock ├── now.json └── posts │ └── index.md ├── get-client-link.js ├── index.js ├── package.json └── renovate.json /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | 5 | --- 6 | 7 | **Describe the bug** 8 | A clear and concise description of what the bug is. 9 | 10 | **To Reproduce** 11 | Steps to reproduce the behavior: 12 | 1. Go to '...' 13 | 2. Click on '....' 14 | 3. Scroll down to '....' 15 | 4. See error 16 | 17 | **Expected behavior** 18 | A clear and concise description of what you expected to happen. 19 | 20 | **Screenshots** 21 | If applicable, add screenshots to help explain your problem. 22 | 23 | **Desktop (please complete the following information):** 24 | - OS: [e.g. iOS] 25 | - Browser [e.g. chrome, safari] 26 | - Version [e.g. 22] 27 | 28 | **Smartphone (please complete the following information):** 29 | - Device: [e.g. iPhone6] 30 | - OS: [e.g. iOS8.1] 31 | - Browser [e.g. stock browser, safari] 32 | - Version [e.g. 22] 33 | 34 | **Additional context** 35 | Add any other context about the problem here. 36 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | 5 | --- 6 | 7 | **Is your feature request related to a problem? Please describe.** 8 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 9 | 10 | **Describe the solution you'd like** 11 | A clear and concise description of what you want to happen. 12 | 13 | **Describe alternatives you've considered** 14 | A clear and concise description of any alternative solutions or features you've considered. 15 | 16 | **Additional context** 17 | Add any other context or screenshots about the feature request here. 18 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | package-lock.json 3 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Contributor Covenant Code of Conduct 2 | 3 | ## Our Pledge 4 | 5 | In the interest of fostering an open and welcoming environment, we as 6 | contributors and maintainers pledge to making participation in our project and 7 | our community a harassment-free experience for everyone, regardless of age, body 8 | size, disability, ethnicity, sex characteristics, gender identity and expression, 9 | level of experience, education, socio-economic status, nationality, personal 10 | appearance, race, religion, or sexual identity and orientation. 11 | 12 | ## Our Standards 13 | 14 | Examples of behavior that contributes to creating a positive environment 15 | include: 16 | 17 | * Using welcoming and inclusive language 18 | * Being respectful of differing viewpoints and experiences 19 | * Gracefully accepting constructive criticism 20 | * Focusing on what is best for the community 21 | * Showing empathy towards other community members 22 | 23 | Examples of unacceptable behavior by participants include: 24 | 25 | * The use of sexualized language or imagery and unwelcome sexual attention or 26 | advances 27 | * Trolling, insulting/derogatory comments, and personal or political attacks 28 | * Public or private harassment 29 | * Publishing others' private information, such as a physical or electronic 30 | address, without explicit permission 31 | * Other conduct which could reasonably be considered inappropriate in a 32 | professional setting 33 | 34 | ## Our Responsibilities 35 | 36 | Project maintainers are responsible for clarifying the standards of acceptable 37 | behavior and are expected to take appropriate and fair corrective action in 38 | response to any instances of unacceptable behavior. 39 | 40 | Project maintainers have the right and responsibility to remove, edit, or 41 | reject comments, commits, code, wiki edits, issues, and other contributions 42 | that are not aligned to this Code of Conduct, or to ban temporarily or 43 | permanently any contributor for other behaviors that they deem inappropriate, 44 | threatening, offensive, or harmful. 45 | 46 | ## Scope 47 | 48 | This Code of Conduct applies both within project spaces and in public spaces 49 | when an individual is representing the project or its community. Examples of 50 | representing a project or community include using an official project e-mail 51 | address, posting via an official social media account, or acting as an appointed 52 | representative at an online or offline event. Representation of a project may be 53 | further defined and clarified by project maintainers. 54 | 55 | ## Enforcement 56 | 57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 58 | reported by contacting the project team at checkraiser11@gmail.com. All 59 | complaints will be reviewed and investigated and will result in a response that 60 | is deemed necessary and appropriate to the circumstances. The project team is 61 | obligated to maintain confidentiality with regard to the reporter of an incident. 62 | Further details of specific enforcement policies may be posted separately. 63 | 64 | Project maintainers who do not follow or enforce the Code of Conduct in good 65 | faith may face temporary or permanent repercussions as determined by other 66 | members of the project's leadership. 67 | 68 | ## Attribution 69 | 70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, 71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html 72 | 73 | [homepage]: https://www.contributor-covenant.org 74 | 75 | For answers to common questions about this code of conduct, see 76 | https://www.contributor-covenant.org/faq 77 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | ## Contributing guide 2 | 3 | - Clone the project. 4 | - Use the basic examples to hack on idea. 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Truong Hoang Dung 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Next Routes Middleware 2 | 3 | [![npm version](https://d25lcipzij17d.cloudfront.net/badge.svg?id=js&type=6&v=3.4.0&x2=0)](https://www.npmjs.com/package/next-routes-middleware) 4 | 5 | Universal Lambda Routing for Next.JS application 6 | 7 | ## Roadmap for 4.0.0 8 | 9 | 1. [Integrate with existing `now.json` in `now.config.js` for better integration with existing ecosystem](https://github.com/revskill10/next-routes-middleware/issues/13) 10 | 2. [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) 11 | 12 | ## Demo 13 | 14 | [Demo](https://next-routes-middleware-4ari798pi.now.sh/) 15 | 16 | ## Installation 17 | 18 | ``` 19 | npm i --save next-routes-middleware 20 | ``` 21 | 22 | ## Features 23 | 24 | - Replicate `next.js` for Now V2 config to develop locally. 25 | - Support named regular expressions used in `routes`. 26 | - Allow custom routes handling with all Express.js http methods. 27 | - Client-side routing 28 | - Compiling to Now V2 compatible JSON config 29 | 30 | ## Usage 31 | 32 | Step 1: Create your own `now.config.js`: 33 | 34 | ```js 35 | const buildTypes = [ 36 | "@now/next", 37 | "@now/static" 38 | ] 39 | const builds = [ 40 | { 41 | src: "next.config.js", use: buildTypes[0] 42 | }, 43 | { 44 | src: "static", use: buildTypes[1] 45 | } 46 | ] 47 | 48 | const nextRoutes = ['/about'].map(function(item, index) { 49 | return { 50 | src: item, 51 | dest: item, 52 | build: buildTypes[0], 53 | } 54 | }) 55 | 56 | const deployConfig = { 57 | version: 2, 58 | name: "next-routes-middleware", 59 | public: true, 60 | alias: "next-routes-middleware" 61 | } 62 | 63 | 64 | const patterns = { 65 | first: "(?.*)", 66 | uuid: "(?[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}", 67 | slug:"(?[^/]*)", 68 | year:"(?[0-9]{4})", 69 | month:"(?[0-9]{2})", 70 | day:"(?[0-9]{2})" 71 | } 72 | 73 | const routes = [ 74 | { 75 | src: "/favicon.ico", 76 | dest: "static/favicon.ico", 77 | build: buildTypes[0], 78 | }, 79 | ...nextRoutes, 80 | { 81 | src: `/w/${patterns.first}`, 82 | dest: "/work?slug=${first}", 83 | build: buildTypes[0], 84 | }, 85 | { 86 | src: `/resource/${patterns.uuid}`, 87 | dest: "/complex?id=${uuid}", 88 | build: buildTypes[0], 89 | }, 90 | { 91 | src: `/t/${patterns.slug}/${patterns.year}-${patterns.month}-${patterns.day}`, 92 | dest: "/more_complex?day=${day}&month=${month}&year=${year}&slug=${slug}" , 93 | build: buildTypes[0], 94 | }, 95 | { src: "/", dest: "/index", build: buildTypes[0], } 96 | ] 97 | 98 | 99 | module.exports = { 100 | ...deployConfig, 101 | patterns, 102 | builds, 103 | routes, 104 | } 105 | 106 | ``` 107 | 108 | *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. 109 | 110 | Step 2: Using `next-routes-middleware` in your custom `server.js` 111 | 112 | ```js 113 | const express = require('express') 114 | const next = require('next'); 115 | const dev = process.env.NODE_ENV !== 'production'; 116 | const app = next({ dev }); 117 | const routesMiddleware = require('../../index') 118 | const port = parseInt(process.env.PORT, 10) || 3000 119 | const config = require('./now.config.js') 120 | app.prepare().then(() => { 121 | const server = express() 122 | const prefix = "" 123 | routesMiddleware({server, app, config, prefix}) 124 | server.listen(port, (err) => { 125 | if (err) throw err 126 | console.log(`> Ready on http://localhost:${port}`) 127 | }) 128 | }) 129 | 130 | 131 | ``` 132 | 133 | Step 3: Using in `pages/more_complex.js` 134 | 135 | ```js 136 | import {withRouter} from 'next/router' 137 | import Nav from '../components/nav' 138 | const Page = ({router}) => { 139 | const { 140 | query 141 | } = router 142 | 143 | const {day, month, year, slug} = query 144 | return ( 145 | <> 146 |