Skip to content

Instantly share code, notes, and snippets.

@renoirb
Last active June 28, 2022 23:38
Show Gist options
  • Select an option

  • Save renoirb/e201ee73e78ac80e1b7d1bc6dfec6c6f to your computer and use it in GitHub Desktop.

Select an option

Save renoirb/e201ee73e78ac80e1b7d1bc6dfec6c6f to your computer and use it in GitHub Desktop.

Revisions

  1. renoirb revised this gist Nov 15, 2018. 3 changed files with 5 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion hooks-index.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    hooks/index.js
    // file: hooks/index.js

    import render from './render'

    // 'render:context'
    2 changes: 2 additions & 0 deletions hooks-render.js
    Original 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) => {
    1 change: 1 addition & 0 deletions hooks-route-redirect-portal.js
    Original 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)
    *
  2. renoirb revised this gist Nov 15, 2018. 4 changed files with 41 additions and 7 deletions.
    7 changes: 7 additions & 0 deletions hooks-index.js
    Original 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),
    })
    16 changes: 16 additions & 0 deletions hooks-render.js
    Original 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))
    },
    }
    }
    Original 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)
    * 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}
    */
    @@ -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 function projectHooksRouteRedirectPortal(req, res, next) {
    const desiredContextRoot = '/portal'

    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 Location = desiredContextRoot + url.pathname + url.search
    const pathname = url.pathname === null ? '' : url.pathname
    const search = url.search === null ? '' : url.search
    const Location = desiredContextRoot + pathname + search
    res.writeHead(302, {
    Location,
    })
    11 changes: 11 additions & 0 deletions nuxt.config.js
    Original 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),
    }
  3. renoirb created this gist Nov 14, 2018.
    50 changes: 50 additions & 0 deletions hooks-render-setupMiddleware.js
    Original 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()
    }