Versioning a JavaScript single page web app? (quer

2019-08-05 03:54发布

问题:

I have a Single Page App with some static routes such as:

example.org/#!/tools/
example.org/#!/stories/story-1/

These are examples of routes that don't need any versioning. Either the "page" exists, it has been redirected or it is non-existent.

But then I have other resources I'd like to version (because they can have internal states - denoted with a querystring of > 10 parameters):

example.org/#!/tools/population-tool/ + ?a=b&c=d[...]

Because the querystring paramaters might change over time (as the tool might allow more options), I'd like to add a version parameter to these "pages":

example.org/#!/tools/population-tool/ + ?v=1.1&a=b&c=d[...]

So that when a user navigates to the URL without any parameters, the default state and version is automatically added:

example.org/#!/tools/population-tool/ =>
example.org/#!/tools/population-tool/ + version + default state

In case the user decides to share/bookmark this URL, the version paramater will always allow me to remap the paramaters from one version to another.

  1. Can you suggest a better approach?
  2. Perhaps the version should be a part of the url for all routes?

    example.org/#!/v1/tools/population-tool/

  3. Or perhaps versioning the querystring is the wrong approach altogether? Perhaps I should have a method that guesstimates the right "API" based on the parameters given?

Thanks.

回答1:

Guessing the right API from parameters can be tedious and will lead to unnecessary complexities in your routing sub-system. It is much more cleaner to having the version information clearly specified in the URL.

Now whether the approach to be followed is 1 or 2 is much of a matter of taste. In my opinion it depends on how perceivable your application version is to the end user. If your end user does not perceive an change in the front-end (ie. if you are following an MVC architecture and the change is only the Model layer while the view layer remains unchanged then approach 1 is more recommended. Where as if it is important for the user to be aware of which version he is using or if the user-interface visible to end user is different across versions then approach 2 is a better option because it puts the version information in a prominent position in the url.

Another way of looking at it can be asking the question if application state is different between versions. A url (sans the query parameters) represents the application state and in case the answer is true then 2nd approach should be followed. However if the version information is abstracted away from the main application code and is relevant only to certain abstracted sub-sections eg. only a few model classes then approach 1 should be followed.