Does anyone know if Skybrud Redirects
# package-development
t
Does anyone know if Skybrud Redirects works with the new Content Delivery API?
a
From what I can tell, this is not something that is supported by Umbraco. Although it probably should be. In Umbraco, the
ByRouteContentApiController
class is responsible for looking up a content node from it's path/route. https://github.com/umbraco/Umbraco-CMS/blob/contrib/src/Umbraco.Cms.Api.Delivery/Controllers/ByRouteContentApiController.cs#L110-L113 If a requested path isn't found, Umbraco asks it's
IRequestRedirectService
if a redirect exists for this route, and if so, returns a 301 redirect to that URL. The default implementation will only look for Umbraco's internal redirects. https://github.com/umbraco/Umbraco-CMS/blob/contrib/src/Umbraco.Cms.Api.Delivery/Services/RequestRedirectService.cs Theoretically you might be able to replace the default implementation with your own, and then look for Skybrud Redirects as well. But the
GetRedirectRoute
method must return an instance of
IApiContentRoute
, so this is tied to content. Eg. you can't support redirects to media or external URLs. The corresponding controller for media doesn't support redirects - probably because Umbraco doesn't have internal redirects for media - only for content. https://github.com/umbraco/Umbraco-CMS/blob/contrib/src/Umbraco.Cms.Api.Delivery/Controllers/ByPathMediaApiController.cs Ideally the
ByRouteContentApiController
class should have an event/notification that developers and packages can hook into. This would let a package like Skybrud Redirects check whether a redirect exists for the specified path, and then handle it accordingly. But this requires changes to Umbraco. Umbraco also handles content and media individually, so if you'd use Skybrud Redirects for creating
/my-fancy-pdf
and redirect users to the actual PDF URL, should this be handled by content API or the media API? You might not know what's behind the path ahead of asking the API(s) 😮
t
@Anders Bjerner My use case is (perhaps obvious) Umbraco as headless CMS in this case, so I'm checking to see if we can use the redirect service for the editors. Given the complications above, I'm considering a workaround for how requests are resolved: - First check with content api as usual. If found, great, if 404, go to step 2: - Make a request to a custom controller that invokes a lookup specifically on Skybrud Redirects to see if/what a possible redirect would be. The drawback is having to write that controller and interface with the redirects, but my guess is that would be very straightforward. Would you say that's a reasonable approach?
a
It should require that much.
IRedirectsService
should have the logic that you need.
The basics could look something like this:
Copy code
csharp
using Skybrud.Umbraco.Redirects.Models;
using Skybrud.Umbraco.Redirects.Services;
using Umbraco.Cms.Web.Common.Controllers;

namespace Umbraco13 {

    public class RedirectsController : UmbracoApiController {

        private readonly IRedirectsService _redirectsService;

        public RedirectsController(IRedirectsService redirectsService) {
            _redirectsService = redirectsService;
        }

        public object? GetByPath(string path) {

            IRedirect? redirect = _redirectsService.GetRedirectByUrl(Guid.Empty, path);

            return redirect ?? (object) NotFound();

        }

    }

}
So a call to
/umbraco/api/Redirects/GetByPath?path=/hi
would trigger the following response:
Copy code
json
{
  "id": 2,
  "key": "10987395-e3cf-4a18-8ee5-058a08a5faaf",
  "rootKey": "00000000-0000-0000-0000-000000000000",
  "path": "/hi",
  "queryString": "",
  "url": "/hi",
  "destination": {
    "id": 1082,
    "key": "614b6874-e078-4879-a9be-4275164df9d1",
    "name": "",
    "url": "/hej-verden/",
    "query": "",
    "fragment": "",
    "fullUrl": "/hej-verden/",
    "type": "content",
    "culture": ""
  },
  "createDate": "2023-11-27T15:25:35.220Z",
  "updateDate": "2023-11-27T15:25:35.220Z",
  "type": "temporary",
  "permanent": false,
  "forward": false
}
It's the redirect model returned as JSON. So at least for this quick controller, the frontend would then have to piece the destination URL back together on it's own.
t
Beautiful, this will help a lot, thank you!
@Anders Bjerner Side question: do you have scheduled plans for upcoming v13 support? I assume it's first after 13 is actually released? I tried using the current one on 13, just out of curiosity, and thought I could bypass the version mismatch (
(>= 10.0.0 && < 12.999.0)
) but I'm not hacker enough to even try 😄
a
The Umbraco 10 package works fine with Umbraco 13 if you can get around the version restriction. But I also have a local build for Umbraco 13. I haven't changed much - IIRC it was all replacing obsolete logic with new corresponding logic. I haven't gotten around to pushing anything on NuGet though 😊
@TackleMcClean 🏅 you can get the Umbraco 13 pre-release here https://github.com/skybrud/Skybrud.Umbraco.Redirects/releases/tag/v13.0.0-alpha001
t
much obliged good sir!
17 Views