2019년 12월 12일
Nextjs에서 styled-components (이하 SC) 사용하면 CSS 로딩이 늦게 되어 발생하는 깜빡임 현상이 발생합니다.
개인적인 생각으로는 HTML은 SSR로 미리 렌더링 시켜 보내지만 SC의 스타일들은 사용자가 접속 시 변환된다고 생각합니다.
Next.js와 SC를 검색한 결과, ServerStyleSheet라는 함수를 SC에서 제공하고 있었고 Next.js에서 샘플 코드를 제공하고 있었습니다.
해당 페이지에서 제공하는 코드는 다음과 같습니다.
import Document from "next/document";
import { ServerStyleSheet } from "styled-components";
export default class MyDocument extends Document {
static async getInitialProps(ctx) {
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();
}
}
}
_document.js 란?
HTML Document를 관리하는 부분으로 이 외에도'_app.js, _error.js'
등이 있습니다.
시간이 지나고 작성하는 글이라 정확하게 기억은 안나지만, 위에 코드를 사용해도 깜빡임 현상이 계속 발생했던 것으로 기억납니다. 그래서 코드를 참고하여 새롭게 개발했습니다.
import Document, { Head, Main, NextScript } from "next/document";
import { ServerStyleSheet } from "styled-components";
import React from "react";
export default class MyDocument extends Document {
static getInitialProps({ renderPage }) {
const sheet = new ServerStyleSheet();
const page = renderPage(
(App) => (props) => sheet.collectStyles(<App {...props} />)
);
const styleTags = sheet.getStyleElement();
return { ...page, styleTags };
}
render() {
return (
<html>
<Head>
<meta data-react-helmet='true' property='og:image' content='' />
{this.props.styleTags}
</Head>
<body>
<Main />
<NextScript />
</body>
</html>
);
}
}
ServerStyleSheet를 사용하여 element로 변환 후 head 태그 안에 출력
했습니다.
2019년 12월 19일
Google Search Console에서는 Google 검색에 콘텐츠를 표시하기 위한 실행 가능 보고서, 도구, 학습 리소스를 제공합니다. [Link](https://support.google.com/webmasters/answer/6258314?utm_source=wnc_376106&utm_medium=gamma&utm_campaign=wnc_376106&utm_content=msg_743502&hl=ko) Google Search Console에서 봇이 데이터를 가져가기 위해 sitemap과 robots의 설정이 필요하며, 먼저 설명드리겠습니다. > 작성 당시 jekyll 버전은 `3.8.6` 입니다. ## Jekyll Sitemap 설치 ### gem jekyll-sitemap 설치 ```bash gem install jekyll-sitemap ``` ### Gemfile 설정 ```ruby # GemFile group :jekyll_plugins do gem "jekyll-sitemap" ``` ### \_config.yml 수정 ```ruby # _config.yml plugins: - jekyll-sitemap ``` > 이제 도메인 뒤에 `/sitemap.xml`를 붙여 설정된 것을 확인합니다. > ex) [https://minhyeong-jang.github.io/sitemap.xml](https://minhyeong-jang.github.io/sitemap.xml) ## robots.txt 설정 Bot에게 현재 사이트의 sitemap과 권한 설정 등을 제공합니다. ```ruby User-agent: * Allow: / Sitemap: https://minhyeong-jang.github.io/sitemap.xml ``` ## Google Search Console 생성 이제 마지막으로 [Google Search Console](https://search.google.com/search-console) 홈페이지에서 속성을 생성합니다. 도메인 / URL 접두어로 나눠져있는데, 따로 Github Pages에 도메인을 설정하지 않은 경우 URL 접두어를 선택합니다. 선택 후에는 설명에 따라 인증을 진행합니다. ( 이전에 작성한 [anlytics]({{ site.base_url}}{{ page.previous.url }})를 진행하신 분은 바로 인증이 가능합니다. ) `Sitemaps`...
2019년 12월 19일
Google에서 제공하는 Google Analytics라는 툴을 사용하여 웹 로그를 분석할 수 있습니다. Google Analytics는 앱 또는 웹의 이용자를 분석하여 통계로 제공해주고 있으며, 예로들어 방문 페이지, 이탈률, 세션 시간 등이 있습니다. 또한 Google Ads, Google Search Console 등 관련 서비스와 통합하여 확인할 수 있습니다. ## Google Analytics 설정 우선 [Google Analytics](https://analytics.google.com/)에 접속하여서 계정을 생성합니다. 계정은 여러개 생성이 가능하며, '속성 및 앱'에 페이지 별로 생성이 가능합니다. 저는 Blog라는 계정 하위에 Tistory, Github Page의 링크를 별도로 등록했습니다. 등록이 완료되면 `추적 코드`가 나타나게 되며, 해당 페이지에서 `추적 ID, 태그(js)`를 제공하고 있습니다. > `추적 코드` 탭이 나타나지 않는다면, `좌측 하단 관리 -> 속성 -> 추적정보 -> 추적코드`로 이동합니다. ## Jekyll 설정 위에서 나온 js를 복붙하셔도 상관 없지만, 저같은 경우에 `_config.yml`에 추적 ID 값을 넣었습니다. ```yml google_analytics_id: 추적 ID ``` 그리고 analytics 파일을 생성하여 Google Analytics에서 제공하는 js를 삽입하고 추적 ID 값을 불러옵니다. 아래 코드는 추적 ID 값을 \_config에서 가져오도록 수정한 코드입니다. ```html {{ "{% if site.google_analytics_id...
2019년 12월 12일
ol, li 태그를 사용하면 옆에 숫자가 기본적으로 나타나는데, 해당 숫자에 bold 처리하는 방법입니다. 단순 font-weight 처리 방법 기본적으로 list-style에 bold 처리를 하려면 ol 태그에 적용하면 됩니다. 이후 li 내 태그를 생성하여 normal을 적용합니다. <ol> <li> <p>test</p> </li> <li> <p>test2</p> </li> <li>not work</li> </ol> <style> ol { font-weight: bold; } ol li p { font-weight: normal; } </style> 위 방법의 문제점은 마지막 li처럼 태그 적용이 안되어있으면 같이 bold 처리가 됩니다. count-increment count-increment 방법을 사용하여 list-style을 사용하지 않고 카운터를 작성할 수 있습니다. <ol> <li> <p>test</p> </li> <li> <p>test2</p> </li> <li>not work</li> </ol> <style> ol { margin: 0 0 1.5em; padding: 0; counter-reset: item; } ol > li { margin: 0; padding: 0 0 0 2em; text-indent: -2em; list-style-type: none; counter-increment: item; } ol > li:before { display: inline-block; width: 1em; padding-right: 0.5em; font-weight: bold; text-align: right; content: counter(item) "."; } </style> counter를 사용하여 li:before에 숫자를 직접 작성하는 방법입니다. 단점으로는 list-style처럼 다양한 스타일이...