Skip to content

Commit

Permalink
Added amp page component.
Browse files Browse the repository at this point in the history
  • Loading branch information
seth2810 committed Sep 14, 2022
1 parent 56b684b commit 9ca04c8
Show file tree
Hide file tree
Showing 6 changed files with 6,289 additions and 2 deletions.
21 changes: 21 additions & 0 deletions next-app/components/AmpLayout.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import React from 'react';
import Head from 'next/head';

import cfg from '../data/config.js';

const AmpLayout = ({ title, children }) => {
const fullTitle = title ? `${title} | ${cfg.title}` : cfg.title;

return (
<>
<Head>
<title>{fullTitle}</title>
<link rel="icon" href={cfg.favicon}></link>
<meta name="viewport" content="width=device-width,minimum-scale=1,initial-scale=1" />
</Head>
<div className="markdown md:markdown-lg lg:markdown-xl">{children}</div>
</>
);
};

export default AmpLayout;
2 changes: 1 addition & 1 deletion next-app/components/DefaultLayout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const DefaultLayout = ({ title, children }) => {
<Head>
<title>{fullTitle}</title>
<link rel="icon" href={cfg.favicon}></link>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"></meta>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
</Head>
<Navbar />
<main className="container">{children}</main>
Expand Down
3 changes: 3 additions & 0 deletions next-app/next-sitemap.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ module.exports = {
generateIndexSitemap: false,
changefreq: null,
priority: null,
exclude: [
'*/amp/*'
],
};
7 changes: 6 additions & 1 deletion next-app/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,9 @@ const { i18n } = require('./next-i18next.config.js');

module.exports = {
i18n,
};
experimental: {
amp: {
skipValidation: true,
},
},
};
81 changes: 81 additions & 0 deletions next-app/pages/amp/[name].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import Link from 'next/link';
import { serverSideTranslations } from 'next-i18next/serverSideTranslations';

import { i18n } from '../../next-i18next.config.js';
import { findPost, getPublishedPosts } from '../../api/index.js';
import AmpLayout from '../../components/AmpLayout.jsx';

const AmpPost = ({ post }) => {
if (!post) {
return null;
}

const href = {
pathname: '/[name]',
query: {
name: post.name,
},
}

return (
<AmpLayout title={post.header}>
<h1>{post.header}</h1>
<Link className="full_post_link" href={href}>
Читать в полной версии →
</Link>
</AmpLayout>
);
};

export const config = {
amp: true,
};

export const getStaticProps = async ({ locale, params }) => {
const post = await findPost(params.name, locale);

if (!post) {
return {
notFound: true,
};
}

if (post.redirect_to) {
return {
redirect: {
permanent: true,
destination: post.redirect_to,
},
};
}

return {
props: {
post,
...(await serverSideTranslations(locale, ['common', 'post'])),
},
};
};

export const getStaticPaths = async () => {
const promises = i18n.locales.map(async (locale) => {
const posts = await getPublishedPosts(locale);

return posts
.filter(({ redirect_to }) => !redirect_to)
.map(({ name }) => ({
locale,
params: { name },
}));
});

const allPaths = await Promise.all(promises);
const paths = allPaths.flat();

return {
paths,
fallback: true,
};
};

export default AmpPost;

0 comments on commit 9ca04c8

Please sign in to comment.