Skip to content

Commit 908ff82

Browse files
committed
feat(seo): sitemap and robots.txt
1 parent 36eb408 commit 908ff82

File tree

5 files changed

+128
-7
lines changed

5 files changed

+128
-7
lines changed

.github/workflows/image_build.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,14 @@ jobs:
2929
uses: docker/metadata-action@v5.5.1
3030
with:
3131
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
32+
- name: get current date
33+
run: echo "date=$(date +'%Y-%m-%d')" >> $GITHUB_OUTPUT
3234
- name: image build and push
3335
uses: docker/build-push-action@v6.2.0
3436
with:
3537
context: .
3638
push: true
3739
tags: ${{ steps.meta.outputs.tags }}
3840
labels: ${{ steps.meta.outputs.labels }}
41+
build-args: |
42+
"BUILD_DATE=${{ steps.date.outputs.date }}"

Dockerfile

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
1-
# Use the official Python 3.8 slim image as the base image
1+
ARG BUILD_DATE
2+
23
FROM python:3.10-slim
34

45
WORKDIR /app
56

7+
ENV PORT 5000
8+
ENV BUILD_DATE=$BUILD_DATE
9+
610
# Copy the necessary files and directories into the container
711
COPY static/ ./static/
812
COPY templates/ ./templates/
@@ -12,5 +16,5 @@ COPY app.py requirements.txt ./
1216
# Upgrade pip and install Python dependencies
1317
RUN pip3 install --upgrade pip && pip install --no-cache-dir -r requirements.txt
1418

15-
EXPOSE 5000
16-
CMD ["gunicorn", "app:app", "-b", "0.0.0.0:5000", "-w", "4"]
19+
EXPOSE ${PORT}/tcp
20+
CMD ['gunicorn', 'app:app', '-b', '"0.0.0.0:${PORT}"', '-w', '4']

app.py

+25-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import re
2-
from os import listdir
2+
from os import listdir, getenv
33
from os.path import join, splitext
44
import yaml
5-
from flask import Flask, request, render_template
5+
from flask import Flask, request, Response, render_template, url_for
66
from werkzeug.middleware.proxy_fix import ProxyFix
77
from flask_minify import minify
88

9-
app = Flask(__name__)
9+
DEFAULT_LAST_MOD = getenv('BUILD_DATE')
10+
11+
app = Flask(__name__, static_folder='static', static_url_path='')
1012

1113
minify(app=app, html=True, js=True, cssless=True)
1214

@@ -103,4 +105,23 @@ def contact():
103105

104106
@app.route("/projects", methods=["GET"])
105107
def projects():
106-
return render_template('projects.html.j2')
108+
return render_template('projects.html.j2')
109+
110+
def has_no_empty_params(rule):
111+
defaults = rule.defaults if rule.defaults is not None else ()
112+
arguments = rule.arguments if rule.arguments is not None else ()
113+
return len(defaults) >= len(arguments)
114+
115+
@app.route("/sitemap.xml", methods=["GET"])
116+
def sitemap():
117+
static_pages = []
118+
for rule in app.url_map.iter_rules():
119+
if "GET" in rule.methods and has_no_empty_params(rule):
120+
url = url_for(rule.endpoint, **(rule.defaults or {}))
121+
static_pages.append(url)
122+
123+
_, timeline = get_posts()
124+
xml = render_template('sitemap.xml.j2', host_url=request.host_url[:-1], static_pages=static_pages, blog_posts=timeline, default_last_mod=DEFAULT_LAST_MOD)
125+
r = Response(response=xml, status=200, mimetype="application/xml")
126+
r.headers["Content-Type"] = "text/xml; charset=utf-8"
127+
return r

static/robots.txt

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
User-agent: Mediapartners-Google
2+
Disallow: /
3+
4+
User-agent: SemrushBot
5+
Disallow: /
6+
7+
user-agent: Pinterestbot
8+
disallow: /
9+
10+
User-agent: AhrefsBot
11+
Disallow: /
12+
13+
User-agent: dotbot
14+
Disallow: /
15+
16+
User-agent: Semrush
17+
Disallow: /
18+
19+
User-agent: GPTBot
20+
Disallow: /
21+
22+
User-agent: ChatGPT-User
23+
Disallow: /
24+
25+
User-agent: OAI-SearchBot
26+
Disallow: /
27+
28+
User-agent: PerplexityBot
29+
Disallow: /
30+
31+
User-agent: Amazonbot
32+
Disallow: /
33+
34+
User-agent: ClaudeBot
35+
Disallow: /
36+
37+
User-agent: anthropic-ai
38+
Disallow: /
39+
40+
User-agent: Claude-Web
41+
Disallow: /
42+
43+
User-agent: Omgilibot
44+
Disallow: /
45+
46+
User-Agent: Applebot
47+
Disallow: /
48+
49+
User-agent: Bytespider
50+
Disallow: /
51+
52+
User-agent: Diffbot
53+
Disallow: /
54+
55+
User-agent: ImagesiftBot
56+
Disallow: /
57+
58+
User-agent: Omgili
59+
Disallow: /
60+
61+
User-agent: YouBot
62+
Disallow: /
63+
64+
User-agent: CCBot
65+
Disallow: /
66+
67+
Sitemap: https://pve.dev/sitemap.xml

templates/sitemap.xml.j2

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
3+
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
5+
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
6+
7+
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
8+
9+
{% for page in static_pages %}
10+
<url>
11+
<loc>{{ host_url + page }}</loc>
12+
<lastmod>{{ default_last_mod }}</lastmod>
13+
</url>
14+
{% endfor %}
15+
{% for year in blog_posts %}
16+
{% for month in blog_posts[year]|sort(reverse=True) %}
17+
{% for day in blog_posts[year][month]|sort %}
18+
<url>
19+
<loc>{{ host_url + blog_posts[year][month][day]['link_to_post'] }}</loc>
20+
<lastmod>{{year}}-{{month}}-{{day}}</lastmod>
21+
</url>
22+
{% endfor %}
23+
{% endfor %}
24+
{% endfor %}
25+
</urlset>

0 commit comments

Comments
 (0)