Last active
June 28, 2022 23:38
-
-
Save renoirb/e201ee73e78ac80e1b7d1bc6dfec6c6f to your computer and use it in GitHub Desktop.
Revisions
-
renoirb revised this gist
Nov 15, 2018 . 3 changed files with 5 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,5 @@ // file: hooks/index.js import render from './render' // 'render:context' This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,3 +1,5 @@ // file: hooks/render.js import redirectRootToPortal from './route-redirect-portal' export default (nuxtConfig) => { This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,5 @@ // file: hooks/route-redirect-portal.js /** * Nuxt middleware hook to redirect from / to /portal (or whatever we set in nuxt.config.js router.base) * -
renoirb revised this gist
Nov 15, 2018 . 4 changed files with 41 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,7 @@ hooks/index.js import render from './render' // 'render:context' export default (nuxtConfig) => ({ render: render(nuxtConfig), }) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,16 @@ import redirectRootToPortal from './route-redirect-portal' export default (nuxtConfig) => { const router = Reflect.has(nuxtConfig, 'router') ? nuxtConfig.router : {} const base = Reflect.has(router, 'base') ? router.base : '/portal' return { /** * 'render:setupMiddleware' * {@link node_modules/nuxt/lib/core/renderer.js} */ setupMiddleware(app) { app.use('/', redirectRootToPortal(base)) }, } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,9 +1,7 @@ // file: hooks/route-redirect-portal.js /** * Nuxt middleware hook to redirect from / to /portal (or whatever we set in nuxt.config.js router.base) * * Should be the same version as connect * {@link node_modules/connect/package.json} */ @@ -26,8 +24,8 @@ import parseurl from 'parseurl' * @param {http.ServerResponse} res Node.js internal response [http_class_http_serverresponse] * @param {Function} next middleware callback */ export default (desiredContextRoot) => function projectHooksRouteRedirectPortal(req, res, next) { const desiredContextRootRegExp = new RegExp(`^${desiredContextRoot}`) const _parsedUrl = Reflect.has(req, '_parsedUrl') ? req._parsedUrl : null const url = _parsedUrl !== null ? _parsedUrl : parseurl(req) @@ -40,7 +38,9 @@ export default function projectHooksRouteRedirectPortal(req, res, next) { isNotProperContextRoot, url, }) const pathname = url.pathname === null ? '' : url.pathname const search = url.search === null ? '' : url.search const Location = desiredContextRoot + pathname + search res.writeHead(302, { Location, }) This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,11 @@ // file: nuxt.config.js import hooks from './hooks' // Typical nuxt.config.js, plus... export default { router: { base: '/portal', }, hooks: hooks(this), } -
renoirb created this gist
Nov 14, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,50 @@ /** * Nuxt middleware hook to redirect from / to /portal (or whatever we set in nuxt.config.js router.base) * TODO: Figure out how to get router.base from Nuxt's nuxt.config.js. */ /** * Should be the same version as connect * {@link node_modules/connect/package.json} */ import parseurl from 'parseurl' /** * Connect middleware to handle redirecting to desired Web Applicatin Context Root. * * Notice that Nuxt docs lacks explaning how to use hooks. * This is a sample router to help explain. * * See nice implementation for inspiration: * - https://github.com/nuxt/nuxt.js/blob/dev/examples/with-cookies/plugins/cookies.js * - https://github.com/yyx990803/launch-editor/blob/master/packages/launch-editor-middleware/index.js * * [http_class_http_clientrequest]: https://nodejs.org/api/http.html#http_class_http_clientrequest * [http_class_http_serverresponse]: https://nodejs.org/api/http.html#http_class_http_serverresponse * * @param {http.ClientRequest} req Node.js internal client request object [http_class_http_clientrequest] * @param {http.ServerResponse} res Node.js internal response [http_class_http_serverresponse] * @param {Function} next middleware callback */ export default function projectHooksRouteRedirectPortal(req, res, next) { const desiredContextRoot = '/portal' const desiredContextRootRegExp = new RegExp(`^${desiredContextRoot}`) const _parsedUrl = Reflect.has(req, '_parsedUrl') ? req._parsedUrl : null const url = _parsedUrl !== null ? _parsedUrl : parseurl(req) const startsWithDesired = desiredContextRootRegExp.test(url.pathname) const isNotProperContextRoot = desiredContextRoot !== url.pathname if (isNotProperContextRoot && startsWithDesired === false) { console.log('routeRedirectPortalHandler', { desiredContextRoot, startsWithDesired, isNotProperContextRoot, url, }) const Location = desiredContextRoot + url.pathname + url.search res.writeHead(302, { Location, }) res.end(); } next() }