# Fail build if cross reference (URL) doesn't exist

**URL:** <https://discourse.gohugo.io/t/fail-build-if-cross-reference-url-doesnt-exist/27635>\
**Category:** support\
**Created:** [August 17, 2020, 11:04am UTC](https://discourse.gohugo.io/t/fail-build-if-cross-reference-url-doesnt-exist/27635 "2020-08-17T11:04:03Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Troy](https://avatars.discourse-cdn.com/v4/letter/t/85f322/32.png) [@Troy](https://discourse.gohugo.io/u/Troy)\
**Post date:** [August 17, 2020, 11:04am UTC](https://discourse.gohugo.io/t/fail-build-if-cross-reference-url-doesnt-exist/27635/1 "2020-08-17T11:04:03Z")

</div>

In the docs there is a part about [cross references](https://gohugo.io/content-management/cross-references/). If the referenced document doesn’t exist, the build fails.

What about links like `[My link](/myurl)`? I want that the build fails if `\myurl` (which I defined in front matter with the `url` parameter) doesn’t exist.

Is this possible in Hugo?

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [August 17, 2020, 11:41am UTC](https://discourse.gohugo.io/t/fail-build-if-cross-reference-url-doesnt-exist/27635/2 "2020-08-17T11:41:06Z")

</div>

Yes. Use the [`ref`](https://gohugo.io/functions/ref) or [`relref`](https://gohugo.io/functions/relref) function within a [render hook](https://gohugo.io/getting-started/configuration-markup#markdown-render-hooks).

---

<div class="post-metadata">

**Author:** ![Troy](https://avatars.discourse-cdn.com/v4/letter/t/85f322/32.png) [@Troy](https://discourse.gohugo.io/u/Troy)\
**Post date:** [August 17, 2020, 12:11pm UTC](https://discourse.gohugo.io/t/fail-build-if-cross-reference-url-doesnt-exist/27635/3 "2020-08-17T12:11:05Z")

</div>

I tried

```
<a href="{{ ref .Destination | safeURL }}"{{ if strings.HasPrefix .Destination "http" }} target="_blank" rel="noopener"{{ end }}>{{ .Text | safeHTML }}</a>

```

and

`<a href="{{ .Destination | safeURL | ref }}"{{ if strings.HasPrefix .Destination "http" }} target="_blank" rel="noopener"{{ end }}>{{ .Text | safeHTML }}</a>`

but both didn’t work.

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [August 17, 2020, 1:08pm UTC](https://discourse.gohugo.io/t/fail-build-if-cross-reference-url-doesnt-exist/27635/4 "2020-08-17T13:08:43Z")

</div>

Please post an example of your frontmatter.

---

<div class="post-metadata">

**Author:** ![Troy](https://avatars.discourse-cdn.com/v4/letter/t/85f322/32.png) [@Troy](https://discourse.gohugo.io/u/Troy)\
**Post date:** [August 17, 2020, 2:18pm UTC](https://discourse.gohugo.io/t/fail-build-if-cross-reference-url-doesnt-exist/27635/5 "2020-08-17T14:18:24Z")

</div>

content/topic1/subtopic1/\_index.md

```
---
url: "myurl"
---

```

and I want to link to this page from content/topic33/subtopic23/subsubtopic2/\_index.md (just to illustrate the complex tree).

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [August 17, 2020, 3:16pm UTC](https://discourse.gohugo.io/t/fail-build-if-cross-reference-url-doesnt-exist/27635/6 "2020-08-17T15:16:01Z")

</div>

If you want to link to content/topic1/subtopic1/\_index.md using `[Subtopic 1](/myurl)`, there is no way that I know of to perform error checking on the URL.

If you want to link to content/topic1/subtopic1/\_index.md using `[Subtopic 1](topic1/subtopic1)`, you can perform error checking in a render hook using `ref` or `relref`:

```auto
{{- $url := urls.Parse .Destination -}}
{{- $remoteSchemes := slice "http" "https" "mailto" -}}
{{- $isRemote := in $remoteSchemes $url.Scheme -}}
{{- if $isRemote -}}
  <a href="{{ .Destination }}" target="_blank" rel="noopener">{{ .Text | safeHTML }}</a>
{{- else -}}
  <a href="{{ ref .Page .Destination }}">{{ .Text | safeHTML }}</a>
{{- end -}}

```

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [August 19, 2020, 3:16pm UTC](https://discourse.gohugo.io/t/fail-build-if-cross-reference-url-doesnt-exist/27635/7 "2020-08-19T15:16:20Z")

</div>

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.
