{
  "name": "Weekly broken link checker with email report",
  "nodes": [
    {
      "parameters": {
        "content": "## Broken link checker\nEvery Monday it opens a page, checks every link on it that points to the same site, and emails you a list of broken ones.\n\n1. Set `startUrl` in **Page to scan** (for example your homepage or sitemap page).\n2. Add an SMTP credential to **Email alert** and set the from and to addresses.\n\nA link counts as broken if it returns 400 or higher, or does not answer within 10 seconds. Only links on the same site are checked, up to 200 per run.\n\nFree template by [serverserv](https://serverserv.com/n8n-templates.html). MIT licence.",
        "height": 380,
        "width": 460,
        "color": 7
      },
      "id": "9dca43a4-3375-5d16-8f2b-45cbe3b38ae0",
      "name": "Setup",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -160,
        -320
      ]
    },
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "weeks",
              "triggerAtDay": [
                1
              ],
              "triggerAtHour": 8
            }
          ]
        }
      },
      "id": "643012aa-e289-533b-8a85-923f57049223",
      "name": "Every Monday at 8",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        120
      ]
    },
    {
      "parameters": {
        "jsCode": "const startUrl = 'https://example.com/';\nreturn [{ json: { startUrl } }];"
      },
      "id": "aa2f2085-d7f3-5df1-9cbe-06c64601c3ae",
      "name": "Page to scan",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        220,
        120
      ]
    },
    {
      "parameters": {
        "url": "={{ $json.startUrl }}",
        "options": {
          "redirect": {
            "redirect": {
              "followRedirects": true,
              "maxRedirects": 5
            }
          },
          "response": {
            "response": {
              "responseFormat": "text"
            }
          },
          "timeout": 15000
        }
      },
      "id": "e902bfc9-0fd8-5df9-829e-03bf2634acf6",
      "name": "Get page",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        440,
        120
      ]
    },
    {
      "parameters": {
        "jsCode": "const startUrl = $('Page to scan').first().json.startUrl;\nconst html = String($input.first().json.data || '');\nconst m0 = startUrl.match(/^(https?:)\\/\\/([^\\/?#]+)/i);\nconst proto = m0[1], host = m0[2].toLowerCase(), origin = proto + '//' + m0[2];\nconst baseDir = startUrl.split(/[?#]/)[0].replace(/[^\\/]*$/, '');\nfunction resolve(href) {\n  let abs;\n  if (/^https?:\\/\\//i.test(href)) abs = href;\n  else if (href.startsWith('//')) abs = proto + href;\n  else if (href.startsWith('/')) abs = origin + href;\n  else abs = baseDir + href;\n  const mm = abs.match(/^(https?:\\/\\/[^\\/?#]+)([^?]*)(\\?.*)?$/i);\n  if (!mm) return null;\n  const parts = [];\n  for (const seg of mm[2].split('/')) {\n    if (seg === '..') parts.pop(); else if (seg !== '.') parts.push(seg);\n  }\n  let path = parts.join('/');\n  if (!path.startsWith('/')) path = '/' + path;\n  return mm[1] + path + (mm[3] || '');\n}\nconst seen = new Set();\nconst re = /href\\s*=\\s*[\"']([^\"'#]+)[^\"']*[\"']/gi;\nlet m;\nwhile ((m = re.exec(html)) !== null) {\n  const href = m[1].trim();\n  if (!href || /^(mailto:|tel:|javascript:|data:)/i.test(href)) continue;\n  const url = resolve(href);\n  if (!url) continue;\n  const h = url.replace(/^https?:\\/\\//i, '').split(/[\\/?#]/)[0].toLowerCase();\n  if (h !== host) continue;\n  seen.add(url);\n  if (seen.size >= 200) break;\n}\nreturn [...seen].map(url => ({ json: { url } }));"
      },
      "id": "0907f3b5-ee0e-5e0b-83e2-d7e9cf7a9e0f",
      "name": "Extract links",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        660,
        120
      ]
    },
    {
      "parameters": {
        "url": "={{ $json.url }}",
        "options": {
          "redirect": {
            "redirect": {
              "followRedirects": true,
              "maxRedirects": 5
            }
          },
          "response": {
            "response": {
              "fullResponse": true,
              "neverError": true,
              "responseFormat": "text"
            }
          },
          "timeout": 10000
        }
      },
      "id": "2b0bbe05-09c5-5ea0-9faf-d4cd3b7ecb2c",
      "name": "Check link",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        880,
        120
      ],
      "onError": "continueRegularOutput"
    },
    {
      "parameters": {
        "jsCode": "const links = $('Extract links').all();\nconst broken = [];\n$input.all().forEach((it, i) => {\n  const status = Number(it.json.statusCode) || 0;\n  if (status === 0 || status >= 400) {\n    broken.push((links[i] ? links[i].json.url : 'unknown URL') + '  ->  ' + (status || 'no response'));\n  }\n});\nif (broken.length === 0) return [];\nreturn [{ json: { count: broken.length, checked: links.length, list: broken.join('\\n') } }];"
      },
      "id": "5e3d8b8b-ca86-5904-b870-965fe8dc7ee8",
      "name": "Collect broken",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1100,
        120
      ]
    },
    {
      "parameters": {
        "fromEmail": "alerts@example.com",
        "toEmail": "you@example.com",
        "subject": "=Broken links found: {{ $json.count }}",
        "emailFormat": "text",
        "text": "={{ $json.count }} of {{ $json.checked }} links on {{ $('Page to scan').first().json.startUrl }} are broken:\n\n{{ $json.list }}",
        "options": {}
      },
      "id": "fa620bb9-5636-5f72-b0e9-4d9c1fdbcb80",
      "name": "Email alert",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2.1,
      "position": [
        1320,
        100
      ]
    }
  ],
  "connections": {
    "Every Monday at 8": {
      "main": [
        [
          {
            "node": "Page to scan",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Page to scan": {
      "main": [
        [
          {
            "node": "Get page",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get page": {
      "main": [
        [
          {
            "node": "Extract links",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract links": {
      "main": [
        [
          {
            "node": "Check link",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check link": {
      "main": [
        [
          {
            "node": "Collect broken",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Collect broken": {
      "main": [
        [
          {
            "node": "Email alert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "meta": {
    "templateCredsSetupCompleted": false
  },
  "tags": []
}