Add a slask / at the end of every routes in Nuxt.js





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















For a purpose of SEO i've been asked to add a slash at the end of every routes of my nuxt project. For example myapp.com/company should be myapp.com/company/ Is there a clean way to do that in Nuxt ?










share|improve this question





























    1















    For a purpose of SEO i've been asked to add a slash at the end of every routes of my nuxt project. For example myapp.com/company should be myapp.com/company/ Is there a clean way to do that in Nuxt ?










    share|improve this question

























      1












      1








      1








      For a purpose of SEO i've been asked to add a slash at the end of every routes of my nuxt project. For example myapp.com/company should be myapp.com/company/ Is there a clean way to do that in Nuxt ?










      share|improve this question














      For a purpose of SEO i've been asked to add a slash at the end of every routes of my nuxt project. For example myapp.com/company should be myapp.com/company/ Is there a clean way to do that in Nuxt ?







      vue.js nuxt.js nuxt-edge






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 17 '18 at 12:13









      yoanncooljazzyoanncooljazz

      188214




      188214
























          2 Answers
          2






          active

          oldest

          votes


















          0














          ok i found the solution by writting a redirection in a middleware on serverside, so first i added to nuxt.config.js this :



          serverMiddleware: ["~/servermiddleware/seo.js"],


          then i created this file /servermiddleware/seo.js :



          const redirects = require('../301.json');
          module.exports = function (req, res, next) {
          const redirect = redirects.find(r => r.from === req.url);
          if (redirect) {
          console.log(`redirect: ${redirect.from} => ${redirect.to}`);
          res.writeHead(301, { Location: redirect.to });
          res.end();
          } else {
          next();
          }
          }


          and finally i write the redirections i want in 301.json in the root folder:



          [
          { "from": "/company", "to": "/company/" }
          ]


          edit: a problem stay here because inside the code of the app links stays without slash so the indexation of robot will use it.... i need to find a better solution.






          share|improve this answer


























          • Every url has the redirection of route. Is this a good solution?

            – soarinblue
            Nov 20 '18 at 2:44



















          0














          I am doing the requirement, too. I do the task with the following method, which I do not know it is right.



          Two steps:



          Nginx rewrite the url, that is to add slash of '/' to the end of url, which the url isn't ended with a slash. In this case, the http request is sent to the web server.



          At another case, if the request (or link routing) is routed at the front end, that the request does not send http request to the web server. Then add a middleware file called addSlash.js like this:



          function isThereSlashEnd(path) {
          let isSlash = true
          if (path) {
          let length = path.length
          isSlash = path[length-1] == '/' ? true : false
          console.log('??? path222: ', path, path[length-1], isSlash)
          }
          return isSlash
          }
          export default function({ req, store, route, redirect }) {

          /**
          * Add slash of '/' at the end of url
          */
          let isSlash = isThereSlashEnd(route.fullPath)
          console.log('??? path111: ', isSlash, route.fullPath, process.client)
          if (!isSlash) {
          if (process.client) {
          window.location.href = route.fullPath + '/'
          console.log('??? path: ', isSlash, route.fullPath, process.client, window.location)
          }
          }
          }


          With two steps above, get the task done.






          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52367491%2fadd-a-slask-at-the-end-of-every-routes-in-nuxt-js%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            ok i found the solution by writting a redirection in a middleware on serverside, so first i added to nuxt.config.js this :



            serverMiddleware: ["~/servermiddleware/seo.js"],


            then i created this file /servermiddleware/seo.js :



            const redirects = require('../301.json');
            module.exports = function (req, res, next) {
            const redirect = redirects.find(r => r.from === req.url);
            if (redirect) {
            console.log(`redirect: ${redirect.from} => ${redirect.to}`);
            res.writeHead(301, { Location: redirect.to });
            res.end();
            } else {
            next();
            }
            }


            and finally i write the redirections i want in 301.json in the root folder:



            [
            { "from": "/company", "to": "/company/" }
            ]


            edit: a problem stay here because inside the code of the app links stays without slash so the indexation of robot will use it.... i need to find a better solution.






            share|improve this answer


























            • Every url has the redirection of route. Is this a good solution?

              – soarinblue
              Nov 20 '18 at 2:44
















            0














            ok i found the solution by writting a redirection in a middleware on serverside, so first i added to nuxt.config.js this :



            serverMiddleware: ["~/servermiddleware/seo.js"],


            then i created this file /servermiddleware/seo.js :



            const redirects = require('../301.json');
            module.exports = function (req, res, next) {
            const redirect = redirects.find(r => r.from === req.url);
            if (redirect) {
            console.log(`redirect: ${redirect.from} => ${redirect.to}`);
            res.writeHead(301, { Location: redirect.to });
            res.end();
            } else {
            next();
            }
            }


            and finally i write the redirections i want in 301.json in the root folder:



            [
            { "from": "/company", "to": "/company/" }
            ]


            edit: a problem stay here because inside the code of the app links stays without slash so the indexation of robot will use it.... i need to find a better solution.






            share|improve this answer


























            • Every url has the redirection of route. Is this a good solution?

              – soarinblue
              Nov 20 '18 at 2:44














            0












            0








            0







            ok i found the solution by writting a redirection in a middleware on serverside, so first i added to nuxt.config.js this :



            serverMiddleware: ["~/servermiddleware/seo.js"],


            then i created this file /servermiddleware/seo.js :



            const redirects = require('../301.json');
            module.exports = function (req, res, next) {
            const redirect = redirects.find(r => r.from === req.url);
            if (redirect) {
            console.log(`redirect: ${redirect.from} => ${redirect.to}`);
            res.writeHead(301, { Location: redirect.to });
            res.end();
            } else {
            next();
            }
            }


            and finally i write the redirections i want in 301.json in the root folder:



            [
            { "from": "/company", "to": "/company/" }
            ]


            edit: a problem stay here because inside the code of the app links stays without slash so the indexation of robot will use it.... i need to find a better solution.






            share|improve this answer















            ok i found the solution by writting a redirection in a middleware on serverside, so first i added to nuxt.config.js this :



            serverMiddleware: ["~/servermiddleware/seo.js"],


            then i created this file /servermiddleware/seo.js :



            const redirects = require('../301.json');
            module.exports = function (req, res, next) {
            const redirect = redirects.find(r => r.from === req.url);
            if (redirect) {
            console.log(`redirect: ${redirect.from} => ${redirect.to}`);
            res.writeHead(301, { Location: redirect.to });
            res.end();
            } else {
            next();
            }
            }


            and finally i write the redirections i want in 301.json in the root folder:



            [
            { "from": "/company", "to": "/company/" }
            ]


            edit: a problem stay here because inside the code of the app links stays without slash so the indexation of robot will use it.... i need to find a better solution.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Sep 17 '18 at 14:09

























            answered Sep 17 '18 at 13:17









            yoanncooljazzyoanncooljazz

            188214




            188214













            • Every url has the redirection of route. Is this a good solution?

              – soarinblue
              Nov 20 '18 at 2:44



















            • Every url has the redirection of route. Is this a good solution?

              – soarinblue
              Nov 20 '18 at 2:44

















            Every url has the redirection of route. Is this a good solution?

            – soarinblue
            Nov 20 '18 at 2:44





            Every url has the redirection of route. Is this a good solution?

            – soarinblue
            Nov 20 '18 at 2:44













            0














            I am doing the requirement, too. I do the task with the following method, which I do not know it is right.



            Two steps:



            Nginx rewrite the url, that is to add slash of '/' to the end of url, which the url isn't ended with a slash. In this case, the http request is sent to the web server.



            At another case, if the request (or link routing) is routed at the front end, that the request does not send http request to the web server. Then add a middleware file called addSlash.js like this:



            function isThereSlashEnd(path) {
            let isSlash = true
            if (path) {
            let length = path.length
            isSlash = path[length-1] == '/' ? true : false
            console.log('??? path222: ', path, path[length-1], isSlash)
            }
            return isSlash
            }
            export default function({ req, store, route, redirect }) {

            /**
            * Add slash of '/' at the end of url
            */
            let isSlash = isThereSlashEnd(route.fullPath)
            console.log('??? path111: ', isSlash, route.fullPath, process.client)
            if (!isSlash) {
            if (process.client) {
            window.location.href = route.fullPath + '/'
            console.log('??? path: ', isSlash, route.fullPath, process.client, window.location)
            }
            }
            }


            With two steps above, get the task done.






            share|improve this answer




























              0














              I am doing the requirement, too. I do the task with the following method, which I do not know it is right.



              Two steps:



              Nginx rewrite the url, that is to add slash of '/' to the end of url, which the url isn't ended with a slash. In this case, the http request is sent to the web server.



              At another case, if the request (or link routing) is routed at the front end, that the request does not send http request to the web server. Then add a middleware file called addSlash.js like this:



              function isThereSlashEnd(path) {
              let isSlash = true
              if (path) {
              let length = path.length
              isSlash = path[length-1] == '/' ? true : false
              console.log('??? path222: ', path, path[length-1], isSlash)
              }
              return isSlash
              }
              export default function({ req, store, route, redirect }) {

              /**
              * Add slash of '/' at the end of url
              */
              let isSlash = isThereSlashEnd(route.fullPath)
              console.log('??? path111: ', isSlash, route.fullPath, process.client)
              if (!isSlash) {
              if (process.client) {
              window.location.href = route.fullPath + '/'
              console.log('??? path: ', isSlash, route.fullPath, process.client, window.location)
              }
              }
              }


              With two steps above, get the task done.






              share|improve this answer


























                0












                0








                0







                I am doing the requirement, too. I do the task with the following method, which I do not know it is right.



                Two steps:



                Nginx rewrite the url, that is to add slash of '/' to the end of url, which the url isn't ended with a slash. In this case, the http request is sent to the web server.



                At another case, if the request (or link routing) is routed at the front end, that the request does not send http request to the web server. Then add a middleware file called addSlash.js like this:



                function isThereSlashEnd(path) {
                let isSlash = true
                if (path) {
                let length = path.length
                isSlash = path[length-1] == '/' ? true : false
                console.log('??? path222: ', path, path[length-1], isSlash)
                }
                return isSlash
                }
                export default function({ req, store, route, redirect }) {

                /**
                * Add slash of '/' at the end of url
                */
                let isSlash = isThereSlashEnd(route.fullPath)
                console.log('??? path111: ', isSlash, route.fullPath, process.client)
                if (!isSlash) {
                if (process.client) {
                window.location.href = route.fullPath + '/'
                console.log('??? path: ', isSlash, route.fullPath, process.client, window.location)
                }
                }
                }


                With two steps above, get the task done.






                share|improve this answer













                I am doing the requirement, too. I do the task with the following method, which I do not know it is right.



                Two steps:



                Nginx rewrite the url, that is to add slash of '/' to the end of url, which the url isn't ended with a slash. In this case, the http request is sent to the web server.



                At another case, if the request (or link routing) is routed at the front end, that the request does not send http request to the web server. Then add a middleware file called addSlash.js like this:



                function isThereSlashEnd(path) {
                let isSlash = true
                if (path) {
                let length = path.length
                isSlash = path[length-1] == '/' ? true : false
                console.log('??? path222: ', path, path[length-1], isSlash)
                }
                return isSlash
                }
                export default function({ req, store, route, redirect }) {

                /**
                * Add slash of '/' at the end of url
                */
                let isSlash = isThereSlashEnd(route.fullPath)
                console.log('??? path111: ', isSlash, route.fullPath, process.client)
                if (!isSlash) {
                if (process.client) {
                window.location.href = route.fullPath + '/'
                console.log('??? path: ', isSlash, route.fullPath, process.client, window.location)
                }
                }
                }


                With two steps above, get the task done.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 23 '18 at 8:03









                soarinbluesoarinblue

                441514




                441514






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52367491%2fadd-a-slask-at-the-end-of-every-routes-in-nuxt-js%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    How to send String Array data to Server using php in android

                    Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                    Is anime1.com a legal site for watching anime?