Skip to content

Design: a better redirect solution #194

@xinbenlv

Description

@xinbenlv

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    eng designIssues requiring engineering designenhancementNew feature or requestinfraInfrastructure

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions