Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Design: a better redirect solution #194

Open
xinbenlv opened this issue Jul 14, 2020 · 1 comment
Open

Design: a better redirect solution #194

xinbenlv opened this issue Jul 14, 2020 · 1 comment
Labels
eng design Issues requiring engineering design enhancement New feature or request infra Infrastructure

Comments

@xinbenlv
Copy link
Owner

xinbenlv commented Jul 14, 2020

As a shortlink service, the key function is the redirect. For example

let's assume our endpoint is zgzg.link. When user visits zgzg.link/some-existing-link, it should redirect the user to the long link by querying what's stored under the goLink="some-existing-link". When user visits zgzg.link/some-nonexisting-link, it should redirect the user to the /edit/some-nonexisting-link page so user can start immediately creating the longer form of links.

The current technical solution is here

// https://github.com/xinbenlv/open-golinks/blob/a71c7d6d792c6545723d3d05f5914e2762852af5/store/index.ts#L24
 async nuxtServerInit({ commit, state, }, ctx2) {
    if (['/edit','/dashboard'].indexOf(ctx2.route.path) >= 0) {
      return;
    } // TODO(xinbenlv@): This is pretty buggy, we shall find better solution
    let goLink = ctx2.params.goLink;
    if (goLink && new RegExp(GOLINK_PATTERN).test(goLink)) {
      let linkItems = await this.$axios.$get(`/api/v2/link/${goLink}`);
      if (linkItems.length == 1) {
        commit('setLinkItem', linkItems[0]);
      } else {
        ctx2.redirect(`/edit/${goLink}`);
      }
    } else {
      ctx2.redirect(`/edit`);
    }
    if(ctx2.req.user) {
      commit('setUser', ctx2.req.user);
      commit('setUserId', ctx2.req.user.emails[0].value)
    }
  }

It might not be ideal to conduct such redirect behavior at nuxtServerInit because it already start the rendering. I think it could happen in the express routing level, which provide better caching for load testing, simplified route configuration, avoid the buggy and hacky solution like

if (['/edit','/dashboard'].indexOf(ctx2.route.path) >= 0) {
      return;
    } // TODO(xinbenlv@): This is pretty buggy, we shall find better solution
@xinbenlv
Copy link
Owner Author

This is an open-ended eng design for you, if you are interested, you can start working on it after you resolve #150, @crystalxshi , I think it would be fun and beneficial for practicing real-world software design.

@xinbenlv xinbenlv added enhancement New feature or request infra Infrastructure eng design Issues requiring engineering design labels Jul 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
eng design Issues requiring engineering design enhancement New feature or request infra Infrastructure
Projects
None yet
Development

No branches or pull requests

2 participants