Exploration for understanding what is the best way to manage routing in Flask in terms of performance, maintainability, use cases.
Started with a question on stackoverflow.
Flask allows for redirect. It helps for managing operational redirects. But I haven't found any elegant way to manage URI persistence (aka the fabric of time) on a long term.
from flask import redirect
@app.route('/shiny')
def shiny_web():
'''DEPRECATED.'''
return redirect('/new_black', 301)
@app.route('/new_black')
def new_black():
'''This is the new feature.'''
return render_template('cool_new_stuff.html')
That's a way to manage it. Though I have the feeling that little by little the code will be crumpled with old def here and there and that might create a maintenance issue on the long term. Cool URIs don't break.
Maybe another way could be (I haven't tested yet, putting my thoughts down).
from flask import redirect
@app.route('/new_black')
@app.route('/shiny', defaults={'status': 301})
def new_black():
'''This is the new feature.'''
if status == 301:
return redirect('/new_black', 301)
return render_template('cool_new_stuff.html')
I could also imagine a module where you could track with a counter how many times the old URL is being accessed on a daily basis. And at a point when it is not accessed anymore for a long period of time you could decide to send a 410 Gone instead of the 301 Permanent Redirect.
I also can imagine a file, something like legacy.url with inside a syntax managing the routing. That might create other types of performance issues.
301 /shiny /new_black
410 /there
- Have you thought about this in the context of Flask?
- What was your strategy?
- Have you experience performance impact by choosing one strategy over another one?