Skip to content

Commit 0710e99

Browse files
committed
4.0 > 4.1: full project migration from JavaScript to TypeScript & many bug fixes
1 parent 9261871 commit 0710e99

File tree

228 files changed

+5132
-1921
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

228 files changed

+5132
-1921
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ Here you can find the code and documentation of my minimalist, static, MDX-based
6666
- [Contribution](#contribution)
6767
- [To do](#to-do)
6868
- [Development](#development)
69+
- [Troubleshooting](#troubleshooting)
70+
- [Type checking](#type-checking)
71+
- [Type errors with third-party libraries](#type-errors-with-third-party-libraries)
72+
- [JSX errors](#jsx-errors)
6973
- [Copyright](#copyright)
7074
- [Licensing](#licensing)
7175
- [Animated emojis](#animated-emojis)
@@ -257,6 +261,28 @@ Feature suggestions and especially bug/typo reports are very welcome. Feel free
257261
> (про выделение памяти и что по умолчанию 5 GB, как избежать проблем) -->
258262

259263

264+
## Troubleshooting
265+
266+
### Type checking
267+
268+
Run `npm run check-types` for tracking TypeScript-specific errors.
269+
270+
TypeScript config is strict, with `noImplicitAny` option enabled.
271+
272+
### Type errors with third-party libraries
273+
274+
When encountering type errors with third-party libraries, it's probably useful to:
275+
1. check if @types packages are available: `npm install --save-dev @types/package-name`
276+
2. add custom type declarations in `src/types/module-declarations.d.ts`
277+
278+
### JSX errors
279+
280+
In case of JSX-related errors, verify that:
281+
1. the TS configuration has `jsx` set correctly
282+
2. files using JSX have `.tsx` extension
283+
3. React is imported in files using JSX
284+
285+
260286
## Copyright
261287

262288
### Licensing

gatsby-ssr.js renamed to gatsby-browser.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import React from 'react';
2+
import type { GatsbyBrowser } from 'gatsby';
23
import Layout from './src/components/layout';
34
//import { LocationProvider } from '@reach/router';
45

5-
export const onRenderBody = ({ setHeadComponents }) => {
6+
export const onRenderBody = ({ setHeadComponents }: { setHeadComponents: (components: React.ReactNode[]) => void }): void => {
67
setHeadComponents([
78
<meta key="charset" charSet="utf-8" />,
89
<meta
@@ -13,12 +14,12 @@ export const onRenderBody = ({ setHeadComponents }) => {
1314
]);
1415
};
1516

16-
export const wrapPageElement = ({ element, props }) => {
17-
// prevent undefined pages from wrapping twice since 404.js redirects to NotFound component
17+
export const wrapPageElement: GatsbyBrowser['wrapPageElement'] = ({ element, props }) => {
18+
// prevent undefined pages from wrapping twice since 404.tsx redirects to NotFound component
1819
if (props.pageContext?.is404) {
1920
return element
2021
}
21-
// wrap all other pages with layout.jsx
22+
// wrap all other pages with layout.tsx
2223
return <Layout {...props}>{element}</Layout>
2324
};
2425

@@ -31,17 +32,17 @@ export const wrapPageElement = ({ element, props }) => {
3132
);
3233
};*/
3334

34-
export const shouldUpdateScroll = () => false;
35+
export const shouldUpdateScroll: GatsbyBrowser['shouldUpdateScroll'] = () => false;
3536

36-
export const onRouteUpdate = ({ location }) => {
37+
export const onRouteUpdate: GatsbyBrowser['onRouteUpdate'] = ({ location }) => {
3738
const exemptPaths = [
3839
'/adventures',
3940
'/research',
4041
'/thoughts'
4142
];
4243
// prevent from scrolling on top on specific pages to save position after reload
4344
const shouldScroll = !exemptPaths.some(path => location.pathname.startsWith(path));
44-
if (shouldScroll) {
45+
if (shouldScroll && typeof window !== 'undefined') {
4546
window.scrollTo({ top: 0, behavior: "smooth" });
4647
}
4748
};

gatsby-config.js renamed to gatsby-config.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1-
module.exports = {
1+
import type { GatsbyConfig } from "gatsby";
2+
3+
interface PageWithFrontmatter {
4+
path: string;
5+
frontmatter?: {
6+
slug: string;
7+
prioritySitemap?: string;
8+
changefreqSitemap?: string;
9+
};
10+
}
11+
12+
const config: GatsbyConfig = {
213
siteMetadata: {
314
title: `avrtt.blog`,
415
description: `My personal all-in-one corner of the internet — a reinvention of old school blogging, community hub, info page, data science & AI learning platform.`,
@@ -27,8 +38,8 @@ module.exports = {
2738
`,
2839
feeds: [
2940
{
30-
serialize: ({ query: { site, allMdx } }) => {
31-
return allMdx.nodes.map(node => {
41+
serialize: ({ query: { site, allMdx } }: { query: { site: any, allMdx: any } }) => {
42+
return allMdx.nodes.map((node: any) => {
3243
return Object.assign({}, node.frontmatter, {
3344
description: node.excerpt,
3445
date: node.frontmatter.date,
@@ -93,25 +104,29 @@ module.exports = {
93104
resolvePages: ({
94105
allSitePage: { nodes: allPages },
95106
allMdx: { nodes: allMdxNodes },
107+
}: {
108+
allSitePage: { nodes: Array<{ path: string }> };
109+
allMdx: { nodes: Array<{ frontmatter: { slug: string; prioritySitemap?: string; changefreqSitemap?: string } }> };
96110
}) => {
97111
return allPages.map(page => {
112+
const pageWithFrontmatter = page as PageWithFrontmatter;
98113
const mdxNode = allMdxNodes.find(mdx =>
99114
mdx.frontmatter.slug === page.path.replace(/\/+$/, "")
100115
)
101116
if (mdxNode) {
102-
page.frontmatter = mdxNode.frontmatter
117+
pageWithFrontmatter.frontmatter = mdxNode.frontmatter;
103118
}
104-
return page
119+
return pageWithFrontmatter;
105120
})
106121
},
107-
serialize: (page) => {
122+
serialize: (page: PageWithFrontmatter) => {
108123
const isAboutPage = (page.path === "/about/" || page.path === "/about")
109124
const changefreq = isAboutPage
110125
? "yearly"
111126
: page.frontmatter?.changefreqSitemap || "weekly"
112127
const priority = isAboutPage
113128
? 1.0
114-
: parseFloat(page.frontmatter?.prioritySitemap) || 0.4
129+
: parseFloat(page.frontmatter?.prioritySitemap || "0.4") || 0.4
115130
return {
116131
url: `https://avrtt.github.io${page.path}`,
117132
changefreq,
@@ -217,4 +232,6 @@ module.exports = {
217232
},
218233
},
219234
],
220-
}
235+
};
236+
237+
export default config;

gatsby-node.js

Lines changed: 0 additions & 152 deletions
This file was deleted.

0 commit comments

Comments
 (0)