Closed4
Gatsbyで作ったブログをNext.jsに移行する
resetCSSを当てる
pages配下に_app.tsxを作成。
下記の通り記載。
_app.tsx
import React from 'react';
// reset.css
import '../src/index.css';
import { AppProps } from 'next/app';
export default function MyApp({ Component, pageProps }: AppProps) {
return <Component {...pageProps} />;
}
参考
styled-componentsを使う
Gatsbyの頃はstyled-componentsを使っていたので、とりあえずそのまま移行する。
Nextだとそのままではstyled-componentsのstyleが当たらなかったので、
pages配下に_document.tsxを作成する。
_document.tsx
import Document, { DocumentContext } from 'next/document';
import { ServerStyleSheet } from 'styled-components';
export default class MyDocument extends Document {
static async getInitialProps(ctx: DocumentContext) {
const sheet = new ServerStyleSheet();
const originalRenderPage = ctx.renderPage;
try {
ctx.renderPage = () =>
originalRenderPage({
enhanceApp: App => props => sheet.collectStyles(<App {...props} />),
});
const initialProps = await Document.getInitialProps(ctx);
return {
...initialProps,
styles: (
<>
{initialProps.styles}
{sheet.getStyleElement()}
</>
),
};
} finally {
sheet.seal();
}
}
}
参考
所管
Gatsbyはデータの取得が楽。
画像をアスペクト比保ったままリサイズしてくれたり、必要なデータの取得は自力で取得する必要なく圧倒的に楽。
Can't resolve 'fs'が出た時の対処
このスクラップは2021/01/13にクローズされました