Skip to content

Commit

Permalink
fix(seo): use absolute URLs as canonical links
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-kashitsyn committed Jan 17, 2024
1 parent 3692d0c commit e820920
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 24 deletions.
28 changes: 16 additions & 12 deletions blogware/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ var SiteLayout = []LayoutEntry{
{Path: "/index.html", Type: IndexPage},
}

func handleAtomRender(w http.ResponseWriter, r *http.Request) {
func handleAtomRender(w http.ResponseWriter, _ *http.Request) {
articles, err := AllArticles()
if err != nil {
w.WriteHeader(500)
fmt.Fprintf(w, "Failed to list posts: %v", err)
return
}
feed, err := RenderAtomFeed("https://mmapped.blog", articles)
feed, err := RenderAtomFeed(rootURL, articles)
if err != nil {
w.WriteHeader(500)
fmt.Fprintf(w, "Failed to render atom feed: %v", err)
Expand Down Expand Up @@ -125,15 +125,16 @@ func renderPostAt(i int, articles []Article) (contents []byte, err error) {
nextPost = &articles[i+1]
}
ctx := PostRenderContext{
Title: article.Title,
CreatedAt: article.CreatedAt,
ModifiedAt: article.ModifiedAt,
Keywords: article.Keywords,
URL: article.URL,
Toc: toc,
Body: body,
PrevPost: prevPost,
NextPost: nextPost,
AbsoluteURL: rootURL + article.URL,
Title: article.Title,
CreatedAt: article.CreatedAt,
ModifiedAt: article.ModifiedAt,
Keywords: article.Keywords,
URL: article.URL,
Toc: toc,
Body: body,
PrevPost: prevPost,
NextPost: nextPost,
}
tmpl, err := getTemplate("post")
if err != nil {
Expand Down Expand Up @@ -333,6 +334,9 @@ func copyFile(src, dst string) error {

func copyRecursively(src, dst string) error {
return filepath.Walk(src, func(p string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if p == src {
return nil
}
Expand Down Expand Up @@ -422,7 +426,7 @@ func RenderSite() error {
}
case AtomXMLFeed:
log.Printf("Rendering %s", e.Path)
feed, err := RenderAtomFeed("https://mmapped.blog", articles)
feed, err := RenderAtomFeed(rootURL, articles)
if err != nil {
return fmt.Errorf("failed to render the atom feed: %w", err)
}
Expand Down
3 changes: 2 additions & 1 deletion blogware/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var (
inputDir string
outputDir string
renderFile string
rootURL string
)

func getTemplate(name string) (tmpl *template.Template, err error) {
Expand All @@ -36,7 +37,6 @@ func getTemplate(name string) (tmpl *template.Template, err error) {

func parseArticle(path string) (article Article, err error) {
stream, err := StreamFromFile(path)

if err != nil {
err = fmt.Errorf("failed to read input file %s: %w", path, err)
return
Expand Down Expand Up @@ -96,6 +96,7 @@ func main() {
flag.StringVar(&inputDir, "input", ".", "the path to the source root")
flag.StringVar(&outputDir, "output", "", "the destination directory for static pages")
flag.StringVar(&renderFile, "f", "", "render the specified file to stdout and exit")
flag.StringVar(&rootURL, "root", "https://mmapped.blog", "the root URL of the blog")

flag.Parse()

Expand Down
21 changes: 11 additions & 10 deletions blogware/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,17 @@ type PageRenderContext struct {
}

type PostRenderContext struct {
Title string
Subtitle string
CreatedAt time.Time
ModifiedAt time.Time
Keywords []string
URL string
Toc []TocSection
Body template.HTML
PrevPost *Article
NextPost *Article
AbsoluteURL string
Title string
Subtitle string
CreatedAt time.Time
ModifiedAt time.Time
Keywords []string
URL string
Toc []TocSection
Body template.HTML
PrevPost *Article
NextPost *Article
}

func (a *Article) Toc() (sections []TocSection, err error) {
Expand Down
2 changes: 1 addition & 1 deletion post.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<link rel="icon" href="/images/favicon.svg">
<link rel="mask-icon" href="/images/mask-icon.svg" color="#000000">
<link rel="alternate" type="application/atom+xml" href="/feed.xml">
<link rel="canonical" href="{{ .URL }}">
<link rel="canonical" href="{{ .AbsoluteURL }}">
</head>
<body>
<article>
Expand Down

0 comments on commit e820920

Please sign in to comment.