Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Gatsby migration fixes #1068

Merged
merged 15 commits into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ if (process.env.CONTEXT === 'production') {
})
}

if (process.env.ANALYZE) {
plugins.push({
resolve: 'gatsby-plugin-webpack-bundle-analyzer',
options: {
analyzerPort: 4000,
production: process.env.NODE_ENV === 'production'
}
})
}

module.exports = {
plugins,
siteMetadata: {
Expand Down
18 changes: 16 additions & 2 deletions gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,24 @@ exports.createPages = async ({ graphql, actions }) => {
})
}

const notFoundRegexp = /^\/404/
const trailingSlashRegexp = /\/$/

exports.onCreatePage = ({ page, actions }) => {
if (/^\/404/.test(page.path)) {
const newPage = { ...page, context: { ...page.context, is404: true } }
let newPage = page

if (notFoundRegexp.test(newPage.path)) {
newPage = { ...newPage, context: { ...newPage.context, is404: true } }
}

if (page.path !== '/' && trailingSlashRegexp.test(newPage.path)) {
newPage = {
...newPage,
path: newPage.path.replace(trailingSlashRegexp, '')
}
}

if (newPage !== page) {
actions.deletePage(page)
actions.createPage(newPage)
}
Expand Down
7 changes: 0 additions & 7 deletions middleware/notFound/index.js

This file was deleted.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"url": "git+https://github.com/iterative/dvc.org.git"
},
"author": "",
"license": "ISC",
"license": "Apache-2.0",
"bugs": {
"url": "https://github.com/iterative/dvc.org/issues"
},
Expand All @@ -32,12 +32,14 @@
"@reach/router": "^1.3.1",
"@sentry/browser": "^5.12.1",
"color": "^3.1.2",
"compression": "^1.7.4",
"date-fns": "^2.8.1",
"docsearch.js": "^2.6.3",
"dom-scroll-into-view": "^2.0.1",
"express": "^4.17.1",
"gatsby": "^2.19.21",
"gatsby-link": "^2.2.29",
"gatsby-plugin-webpack-bundle-analyzer": "^1.0.5",
"github-markdown-css": "^3.0.1",
"isomorphic-fetch": "^2.2.1",
"lodash.fill": "^3.4.0",
Expand All @@ -50,6 +52,7 @@
"node-cache": "^5.1.0",
"perfect-scrollbar": "^1.4.0",
"prismjs": "^1.19.0",
"promise-polyfill": "^8.1.3",
"prop-types": "^15.7.2",
"react": "^16.12.0",
"react-collapse": "^5.0.1",
Expand All @@ -64,6 +67,7 @@
"react-use": "^13.24.0",
"rehype-react": "^4.0.1",
"request": "^2.88.0",
"serve-handler": "^6.1.2",
"slick-carousel": "^1.8.1",
"styled-components": "^4.4.1",
"styled-reset": "^4.0.8",
Expand Down
36 changes: 32 additions & 4 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,47 @@
/* eslint-env node */

const express = require('express')
const compression = require('compression')
const serveHandler = require('serve-handler')

const app = express()

const apiMiddleware = require('./middleware/api')
const redirectsMiddleware = require('./middleware/redirects')
const notFoundMiddleware = require('./middleware/notFound')

const port = process.env.PORT || 3000

app.use(compression())
app.use(redirectsMiddleware)
app.use('/api', apiMiddleware)

app.use(express.static('public', { cacheControl: true, maxAge: 0 }))

app.use(notFoundMiddleware)
app.use((req, res) => {
serveHandler(req, res, {
public: 'public',
cleanUrls: true,
trailingSlash: false,
directoryListing: false,
headers: [
{
source: '**/*.@(jpg|jpeg|gif|png)',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fabiosantoscode could you please review this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

headers: [
{
key: 'Cache-Control',
value: 'max-age=86400'
}
]
},
{
source: '!**/*.@(jpg|jpeg|gif|png)',
headers: [
{
key: 'Cache-Control',
value: 'max-age=0'
}
]
}
]
})
})

app.listen(port, () => console.log(`Ready on localhost:${port}!`))
2 changes: 1 addition & 1 deletion src/components/DocLayout/SidebarMenu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export default function SidebarMenu({ id, sidebar, currentPath, onClick }) {
})
}

const node = document.getElementById(currentPath.replace(/\/$/, ''))
const node = document.getElementById(currentPath)
const parent = document.getElementById(id)

setIsScrollHidden(true)
Expand Down
33 changes: 3 additions & 30 deletions src/components/DocLayout/index.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,21 @@
/* global docsearch:readonly */

import React, { useCallback, useEffect, useState } from 'react'
import React, { useCallback, useState } from 'react'
import PropTypes from 'prop-types'
import Hamburger from '../Hamburger'
import SearchForm from '../SearchForm'
import MainLayout from '../MainLayout'
import SidebarMenu from './SidebarMenu'

import { Container, Backdrop, SearchArea, Side, SideToggle } from './styles'
import { Container, Backdrop, Side, SideToggle } from './styles'

import { structure } from '../../utils/sidebar'

const SIDEBAR_MENU = 'sidebar-menu'

function DocLayout({ children, ...restProps }) {
const [isMenuOpen, setIsMenuOpen] = useState(false)
const [isSearchAvaible, setIsSearchAvaible] = useState(false)

const toggleMenu = useCallback(() => setIsMenuOpen(!isMenuOpen), [isMenuOpen])

useEffect(() => {
try {
docsearch

setIsSearchAvaible(true)

if (docsearch && isSearchAvaible) {
docsearch({
apiKey: '755929839e113a981f481601c4f52082',
indexName: 'dvc',
inputSelector: '#doc-search',
debug: false // Set to `true` if you want to inspect the dropdown
})
}
} catch (ReferenceError) {
// nothing there
}
}, [isSearchAvaible])

return (
<MainLayout {...restProps} isDocPage>
<Container>
Expand All @@ -48,12 +26,7 @@ function DocLayout({ children, ...restProps }) {
</SideToggle>

<Side isOpen={isMenuOpen}>
{isSearchAvaible && (
<SearchArea>
<SearchForm />
</SearchArea>
)}

<SearchForm />
<SidebarMenu
sidebar={structure}
currentPath={restProps.location.pathname}
Expand Down
19 changes: 0 additions & 19 deletions src/components/DocLayout/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,6 @@ export const Side = styled.div`
`};
`

export const SearchArea = styled.div`
height: 60px;
display: flex;
align-items: center;
background-color: #eef4f8;
z-index: 10;
position: sticky;
top: 0;

${media.phablet`
position: relative;
padding: 0 20px;
`};

form {
height: 40px;
}
`

export const SideToggle = styled.div`
display: none;
position: fixed;
Expand Down
1 change: 1 addition & 0 deletions src/components/Documentation/Markdown/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ ABBR.propTypes = {

const renderAst = new rehypeReact({
createElement: React.createElement,
Fragment: React.Fragment,
components: { details: Details, abbr: ABBR }
}).Compiler

Expand Down
4 changes: 4 additions & 0 deletions src/components/Documentation/Markdown/styles.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ export const Content = styled.article`
content: url(/img/external-link.svg);
}

.anchor {
margin-left: -24px;
}

pre[class*='language-'] {
background: #40354d;
color: #ccc;
Expand Down
49 changes: 6 additions & 43 deletions src/components/Documentation/RightPanel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import throttle from 'lodash.throttle'

import Tutorials from '../Tutorials'

import { allImagesLoadedInContainer } from '../../../utils/images'

import {
Description,
DiscordButton,
Expand All @@ -17,40 +19,6 @@ import {
const ROOT_ELEMENT = 'bodybag'
const MARKDOWN_ROOT = '#markdown-root'

const imageChecker = (images, callback) => {
// IE can't use forEach on array-likes
const imagesArray = Array.prototype.slice.call(images)

if (imagesArray.length) {
let counter = imagesArray.length

const unsubscribe = () => {
imagesArray.forEach(img => {
img.removeEventListener('load', decrement)
img.removeEventListener('error', decrement)
})
}

const decrement = () => {
counter -= 1

if (!counter) {
callback()
unsubscribe()
}
}

imagesArray.forEach(img => {
img.addEventListener('load', decrement)
img.addEventListener('error', decrement)
})

setTimeout(() => {
if (counter) unsubscribe()
}, 5000)
}
}

export default class RightPanel extends React.PureComponent {
state = {
height: 0,
Expand Down Expand Up @@ -79,15 +47,10 @@ export default class RightPanel extends React.PureComponent {
window.removeEventListener('resize', this.updateHeadingsPosition)
}

initHeadingsPosition = () => {
const images = document.querySelectorAll(`${MARKDOWN_ROOT} img`)

if (images.length) {
imageChecker(images, this.updateHeadingsPosition)
} else {
this.updateHeadingsPosition()
}
}
initHeadingsPosition = () =>
allImagesLoadedInContainer(document.querySelector(MARKDOWN_ROOT)).then(
this.updateHeadingsPosition
)

updateHeadingsPosition = () => {
const coordinates = this.props.headings.reduce((result, { slug }) => {
Expand Down
14 changes: 10 additions & 4 deletions src/components/Layout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,28 @@ import { GlobalStyle } from '../../styles'
import MainLayout from '../MainLayout'
import DocLayout from '../DocLayout'

import { allImagesLoadedInContainer } from '../../utils/images'

import './fonts/fonts.css'

const useAnchorNavigation = () => {
const location = useLocation()

useEffect(() => {
const bodybag = document.getElementById('bodybag')

if (!bodybag) {
return
}

if (location.hash) {
const node = document.querySelector(location.hash)

if (node) {
node.scrollIntoView()
allImagesLoadedInContainer(bodybag).then(() => node.scrollIntoView())
}
} else {
document
.getElementById('bodybag')
.scrollTo({ top: 0, behavior: 'smooth' })
bodybag.scrollTop = 0
}
}, [location.href])
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/SEO/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ function SEO({

const defaultMeta = [
{
property: 'description',
name: 'description',
content: metaDescription
},
{
property: 'keywords',
name: 'keywords',
content: metaKeywords
},
{
Expand Down
Loading