views counter fix

This commit is contained in:
Sergey Yarkov 2023-02-27 23:18:13 +03:00
parent cb8d5b3c6a
commit 2b0adea120
No known key found for this signature in database
GPG Key ID: E91CE1FABC619B6F
3 changed files with 16 additions and 2 deletions

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"githubPullRequests.ignoredPullRequestBranches": ["develop"]
}

View File

@ -1,12 +1,12 @@
--- ---
import type { CollectionEntry } from "astro:content"; import type { CollectionEntry } from "astro:content";
import { Icon } from "astro-icon"; import { Icon } from "astro-icon";
import slugify from "@sindresorhus/slugify";
import { createDateFormatter } from "@root/utils"; import { createDateFormatter } from "@root/utils";
import ArticleCover from "@components/ArticleCover.astro"; import ArticleCover from "@components/ArticleCover.astro";
import ArticleComments from "@components/ArticleComments.astro"; import ArticleComments from "@components/ArticleComments.astro";
import TranslatedLink from "@components/TranslatedLink.astro"; import TranslatedLink from "@components/TranslatedLink.astro";
import { useTranslation, getLanguageFromURL } from "@i18n/utils"; import { useTranslation, getLanguageFromURL } from "@i18n/utils";
import * as utils from "@utils/index";
export type Props = { export type Props = {
article: CollectionEntry<"blog">; article: CollectionEntry<"blog">;
@ -17,9 +17,11 @@ const { article } = Astro.props as Props;
const pageLang = getLanguageFromURL(Astro.url.pathname); const pageLang = getLanguageFromURL(Astro.url.pathname);
const canonicalUrl = new URL(Astro.url.pathname, Astro.site); const canonicalUrl = new URL(Astro.url.pathname, Astro.site);
const { const {
id,
data: { coverImage, title, author, description, tags, pubDate }, data: { coverImage, title, author, description, tags, pubDate },
} = article; } = article;
const slug = slugify(title, { lowercase: true });
const slug = utils.encodeString(id.split("/")[1]);
const t = useTranslation(Astro); const t = useTranslation(Astro);
--- ---
@ -68,6 +70,7 @@ const t = useTranslation(Astro);
<script define:vars={{ slug, IS_DEV: import.meta.env.DEV }}> <script define:vars={{ slug, IS_DEV: import.meta.env.DEV }}>
(async () => { (async () => {
console.log(slug);
if (IS_DEV) return; if (IS_DEV) return;
const response = await fetch(`/api/views?slug=${slug}`, { method: "POST" }); const response = await fetch(`/api/views?slug=${slug}`, { method: "POST" });
if (!response.ok) return; if (!response.ok) return;

View File

@ -15,3 +15,11 @@ export function isURL(url: string): boolean {
export function findDuplicates(array: string[]): string[] { export function findDuplicates(array: string[]): string[] {
return array.filter((item, i) => array.indexOf(item) !== i); return array.filter((item, i) => array.indexOf(item) !== i);
} }
export function encodeString(string: string, slice: number = 10) {
return Buffer.from(string)
.toString("base64")
.slice(0, slice)
.toLocaleLowerCase()
.replace(/[^a-zA-Z]+/g, "");
}