Skip to content

Commit

Permalink
Do not intercept anchors with rel="external" (#238)
Browse files Browse the repository at this point in the history
* disable router with rel="external"

* Add docs
  • Loading branch information
lukechu10 authored Sep 12, 2021
1 parent 2a6e82d commit e030ba4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
12 changes: 12 additions & 0 deletions docs/next/advanced/routing.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,3 +274,15 @@ template! {
}
}
```
## `rel="external"`
By default, the router will intercept all `<a>` elements that have the same origin as the current
page. Sometimes, we just want the browser to handle navigation without being intercepted by the
router. To bypass the router, we can add the `rel="external"` attribute to the anchor tag.
```rust
template! {
a(href="path", rel="external") { "Path" }
}
```
7 changes: 7 additions & 0 deletions packages/sycamore-router/src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ impl Integration for HistoryIntegration {
let location = web_sys::window().unwrap().location();

let a = a.unchecked_into::<HtmlAnchorElement>();

// Check if a has `rel="external"`.
if a.rel() == "external" {
// Use default browser behaviour.
return;
}

let origin = a.origin();
let path = a.pathname();
let hash = a.hash();
Expand Down
1 change: 1 addition & 0 deletions website/src/versions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ fn versioned_docs_link_view(
a(
class="hover:text-yellow-500 transition-colors",
href="/api/sycamore/index.html",
rel="external"
) { "API" }
},
}
Expand Down

0 comments on commit e030ba4

Please sign in to comment.