Skip to content
This repository has been archived by the owner on Feb 7, 2023. It is now read-only.

Commit

Permalink
permanent redirects init
Browse files Browse the repository at this point in the history
  • Loading branch information
landsman committed Jun 29, 2020
1 parent 7213767 commit c58e700
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ A middleware (+ utility tools) for having fully internationalized routes in your
{
supportedLangs: ['en', 'de', 'cs'],
routes: routes,
redirects: []
shouldHandleEmptyRoute: true
}
)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@trisbee/next-i18n-routes-middleware",
"version": "0.1.6",
"version": "0.1.7-beta",
"description": "A middleware (+ utility tools) for having fully internationalized routes in your Next.js apps.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
12 changes: 10 additions & 2 deletions src/middleware/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Request, Response } from 'express';
import { parse } from 'url';
import { getPath, getRouteMatchedObject } from '../utils/routing';
import { getPath, getRouteMatchedObject, getPermanentRedirect } from '../utils/routing';
import { getPatternSupportedLangs } from '../utils/lang';
import { NextFunction } from 'connect';
import { GetNextI18nRoutesMiddleware } from './types';
Expand Down Expand Up @@ -28,6 +28,14 @@ const getNextI18nRoutesMiddleware: GetNextI18nRoutesMiddleware = (
const query = parse(reqEndpoint.url, true).query;
const responseQuery = {...query, lang};

// permanent redirect with 301 from server
const redirectTo = getPermanentRedirect(settings.redirects, path);
if (redirectTo) {
console.log('301 redirect from: `' + path + '` to: `' + redirectTo + '`');
resEndpoint.redirect(301, redirectTo);
return;
}

// Get RouteMatchedObject out of path
const routeMatchedObject = getRouteMatchedObject(settings.routes, path, lang);
if (!routeMatchedObject) {
Expand Down Expand Up @@ -57,4 +65,4 @@ const getNextI18nRoutesMiddleware: GetNextI18nRoutesMiddleware = (
};
};

export { getNextI18nRoutesMiddleware };
export { getNextI18nRoutesMiddleware };
5 changes: 3 additions & 2 deletions src/middleware/types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Route } from '../utils/routing/types';
import {Route, Redirect} from '../utils/routing/types';
import { Application, RequestHandler } from 'express';

interface NextI18nRoutesSettings {
supportedLangs: string[],
routes: Route[],
redirects: Redirect[],
shouldHandleEmptyRoute?: boolean,
}

Expand All @@ -13,4 +14,4 @@ type GetNextI18nRoutesMiddleware = (
settings: NextI18nRoutesSettings
) => RequestHandler;

export { NextI18nRoutesSettings, GetNextI18nRoutesMiddleware }
export { NextI18nRoutesSettings, GetNextI18nRoutesMiddleware }
15 changes: 13 additions & 2 deletions src/utils/routing/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { match as createMatcher } from 'path-to-regexp';
import { Route } from './types';
import { Route, Redirect } from './types';
import { getPathParsed, sliceOutQueryString } from './utils';

const getPath = (url: string): string => {
Expand Down Expand Up @@ -38,4 +38,15 @@ const getRouteMatchedObject = (routes: Route[], path: string, currentLang: strin
};
};

export { getPath, getRouteMatchedObject }
const getPermanentRedirect = (redirects: Redirect[], path: string) => {
let redirectTo = null;
redirects.forEach(redirect => {
if (redirect.from === path) {
redirectTo = redirect.to;
}
});

return redirectTo;
};

export { getPath, getRouteMatchedObject, getPermanentRedirect }
7 changes: 6 additions & 1 deletion src/utils/routing/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ interface Route {
aliases: Aliases
}

interface Redirect {
from: string,
to: string
}

type GetPathParsed = (route: Route, path: string, currentLang: string ) => Match<object>
type SliceOutQueryString = (url: string) => string

export { Aliases, Route, GetPathParsed, SliceOutQueryString }
export { Aliases, Route, Redirect, GetPathParsed, SliceOutQueryString }

0 comments on commit c58e700

Please sign in to comment.