pallets / flask

The Python micro framework for building web applications.

Home Page:https://flask.palletsprojects.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Route Aliases

alluding opened this issue · comments

commented

I think it would be cool if there were built-in aliases for routes, for example:

@route(
    "/information",
    aliases=["/info", "/faq"] # <- They all show the same thing as the main route `/information`.
)

It might be useless to some, but I don't. I think it would be useful to allow people to define multiple aliases for one route. It is possible as of now using this code I made:

from typing import List, Callable, Union
from flask import Flask, wraps, redirect, jsonify

app = Flask(__name__)

def route(name: str, aliases: List[str] = [], **options) -> Callable:
    def decorator(func: Callable) -> Callable:
        @app.route(name, **options)
        @wraps(func)
        async def wrapper(*args, **kwargs) -> Union[redirect, jsonify]:
            return await func(*args, **kwargs)

        [app.route(alias, **options)(wrapper) for alias in aliases]

        return wrapper

    return decorator

Your aliases should probably redirect and not result in multiple URLs doing the same thing... it's generally considered bad to have duplicate content on the web w/o redirects to the canonical URL.
Also, don't abuse list comprehensions where a for loop would be appropriate.

In any case, this is something you can easily implement in your own app if you want it.

commented

Your aliases should probably redirect and not result in multiple URLs doing the same thing... it's generally considered bad to have duplicate content on the web w/o redirects to the canonical URL. Also, don't abuse list comprehensions where a for loop would be appropriate.

In any case, this is something you can easily implement in your own app if you want it.

My code was something I created on the spot. I haven't taken a comprehensive look at it, as you mentioned, duplicate content on the web. I mean, would it be better if I made them all redirect back to the main route?