Skip to content

Commit

Permalink
Removed next-url-prettifier for routing
Browse files Browse the repository at this point in the history
Added next-routes for routing
Updated pages & components
  • Loading branch information
Sly777 committed Aug 7, 2017
1 parent 6555178 commit f61d87b
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 61 deletions.
9 changes: 4 additions & 5 deletions components/LinkList.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import { Link } from 'next-url-prettifier';
import PropTypes from 'prop-types';
import styled from 'styled-components';
import { Router } from '../routes';
import { Link } from '../routes';

const LinkList = ({ className, pathname, authenticated, logout }) =>
<nav className={className}>
<Link prefetch href="/">
<a className={pathname === '/' && 'is-active'}>Main Page</a>
</Link>
<Link prefetch route={Router.linkPage('create')}>
<Link prefetch route="create">
<a className={pathname === '/create_post' && 'is-active'}>Create</a>
</Link>
{!authenticated &&
<Link prefetch route={Router.linkPage('signin')}>
<Link prefetch route="signin">
<a className={pathname === '/sign_in' && 'is-active'}>SignIn</a>
</Link>}
{!authenticated &&
<Link prefetch route={Router.linkPage('signup')}>
<Link prefetch route="signup">
<a className={pathname === '/sign_up' && 'is-active'}>SignUp</a>
</Link>}
{authenticated &&
Expand Down
8 changes: 4 additions & 4 deletions components/PostList.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { gql, graphql } from 'react-apollo';
import PropTypes from 'prop-types';
import styled from 'styled-components';
import { Link } from 'next-url-prettifier';
import { Router } from '../routes';
import { Link } from '../routes';
import PostUpvoter from './PostUpvoter';

const POSTS_PER_PAGE = 10;
Expand All @@ -24,10 +23,11 @@ let PostList = ({
{index + 1}.{' '}
</span>
<Link
route={Router.linkPage('details', {
route="details"
params={{
postId: post.id,
postTitle: encodeURIComponent(post.title)
})}
}}
>
<a>
{post.title}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"lru-cache": "4.1.1",
"moment": "2.18.1",
"next": "3.0.1-beta.19",
"next-url-prettifier": "1.1.5",
"next-routes": "1.0.40",
"offline-plugin": "4.8.3",
"prop-types": "15.5.10",
"react": "15.6.1",
Expand Down
41 changes: 6 additions & 35 deletions routes.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
const UrlPrettifier = require('next-url-prettifier').default;

const routes = [];
const routes = require('next-routes')();

//
// Because of awesome Next.js, You don't need to add routes for all pages.
Expand All @@ -15,40 +13,13 @@ const routes = [];
//
// ------------ ROUTES ---------------

routes.push({
page: 'details',
prettyUrl: ({ postId = '', postTitle = '' }) =>
`/details/${postId}/${postTitle}`,
prettyUrlPatterns: [
{ pattern: '/details/:postId/:postTitle', defaultParams: { lang: 'en' } }
]
});

routes.push({
page: 'create',
prettyUrl: () => '/create_post',
prettyUrlPatterns: [
{ pattern: '/create_post', defaultParams: { lang: 'en' } }
]
});

routes.push({
page: 'signin',
prettyUrl: () => '/sign_in',
prettyUrlPatterns: [{ pattern: '/sign_in', defaultParams: { lang: 'en' } }]
});

routes.push({
page: 'signup',
prettyUrl: () => '/sign_up',
prettyUrlPatterns: [{ pattern: '/sign_up', defaultParams: { lang: 'en' } }]
});
routes.add('details', '/details/:postId/:postTitle');
routes.add('create', '/create_post');
routes.add('signin', '/sign_in');
routes.add('signup', '/sign_up');

// ------------ ROUTES ---------------
//
//

const urlPrettifier = new UrlPrettifier(routes);

exports.default = routes;
exports.Router = urlPrettifier;
module.exports = routes;
21 changes: 9 additions & 12 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const cors = require('cors');
const helmet = require('helmet');
require('dotenv').config();

const Router = require('./routes').Router;
const router = require('./routes');
const logger = require('./server/logger');

const isDev = process.env.NODE_ENV !== 'production';
Expand Down Expand Up @@ -75,6 +75,13 @@ const renderAndCache = function renderAndCache(
});
};

const routerHandler = router.getRequestHandler(
app,
({ req, res, route, query }) => {
renderAndCache(req, res, route.page, query);
}
);

app.prepare().then(() => {
const server = express();

Expand All @@ -87,17 +94,7 @@ app.prepare().then(() => {
})
);
server.use(helmet());

Router.forEachPattern((page, pattern, defaultParams) =>
server.get(pattern, (req, res) =>
renderAndCache(
req,
res,
`/${page}`,
Object.assign({}, defaultParams, req.query, req.params)
)
)
);
server.use(routerHandler);

server.get('/sw.js', (req, res) =>
app.serveStatic(req, res, path.resolve('./.next/sw.js'))
Expand Down
10 changes: 6 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4699,9 +4699,11 @@ netrc@^0.1.4:
version "0.1.4"
resolved "https://registry.yarnpkg.com/netrc/-/netrc-0.1.4.tgz#6be94fcaca8d77ade0a9670dc460914c94472444"

[email protected]:
version "1.1.5"
resolved "https://registry.yarnpkg.com/next-url-prettifier/-/next-url-prettifier-1.1.5.tgz#ce27eb29f584bd53aadf091874138ddd93bc8272"
[email protected]:
version "1.0.40"
resolved "https://registry.yarnpkg.com/next-routes/-/next-routes-1.0.40.tgz#1987ed88be808440f7eb963d3836916f46938e73"
dependencies:
path-to-regexp "^1.7.0"

[email protected]:
version "3.0.1-beta.19"
Expand Down Expand Up @@ -5253,7 +5255,7 @@ [email protected]:
version "0.1.7"
resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-0.1.7.tgz#df604178005f522f15eb4490e7247a1bfaa67f8c"

path-to-regexp@^1.0.0, path-to-regexp@^1.1.1:
path-to-regexp@^1.0.0, path-to-regexp@^1.1.1, path-to-regexp@^1.7.0:
version "1.7.0"
resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-1.7.0.tgz#59fde0f435badacba103a84e9d3bc64e96b9937d"
dependencies:
Expand Down

0 comments on commit f61d87b

Please sign in to comment.