Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Gatsby is a React-based open-source framework for creating websites and apps. It allows developers to build fast, secure, and powerful web experiences using modern web technologies. Gatsby leverages GraphQL for data management and offers a rich plugin ecosystem to extend its functionality.
Static Site Generation
Gatsby can generate static pages from data sources like Markdown files. This code sample demonstrates how to create pages dynamically using GraphQL to query Markdown files and the createPage API to generate pages.
const path = require('path');
exports.createPages = async ({ graphql, actions }) => {
const { createPage } = actions;
const result = await graphql(`
{
allMarkdownRemark {
edges {
node {
frontmatter {
path
}
}
}
}
}
`);
result.data.allMarkdownRemark.edges.forEach(({ node }) => {
createPage({
path: node.frontmatter.path,
component: path.resolve(`src/templates/blog-post.js`),
context: {},
});
});
};
GraphQL Data Layer
Gatsby uses GraphQL to manage data. This example shows how to query site metadata using GraphQL and display it in a React component.
import React from 'react';
import { graphql } from 'gatsby';
export const query = graphql`
query {
site {
siteMetadata {
title
}
}
}
`;
const IndexPage = ({ data }) => (
<div>
<h1>{data.site.siteMetadata.title}</h1>
</div>
);
export default IndexPage;
Plugin Ecosystem
Gatsby has a rich plugin ecosystem that allows developers to extend its functionality. This example shows how to configure plugins for handling React Helmet, sourcing files from the filesystem, and transforming Markdown files.
module.exports = {
plugins: [
`gatsby-plugin-react-helmet`,
{
resolve: `gatsby-source-filesystem`,
options: {
name: `src`,
path: `${__dirname}/src/`,
},
},
`gatsby-transformer-remark`,
],
};
Next.js is a React framework for server-side rendering and static site generation. It offers a more flexible approach to rendering, allowing developers to choose between static generation, server-side rendering, and client-side rendering on a per-page basis. Compared to Gatsby, Next.js is more versatile in terms of rendering options but may require more configuration for static site generation.
Nuxt.js is a framework for creating Vue.js applications with server-side rendering, static site generation, and client-side rendering. It provides a similar feature set to Gatsby but is built on Vue.js instead of React. Nuxt.js is ideal for developers who prefer Vue.js over React.
Gridsome is a Vue.js framework for building static websites and apps. It is similar to Gatsby in that it uses GraphQL for data management and offers a plugin ecosystem. Gridsome is a good alternative for developers who prefer Vue.js and want a static site generator with a similar feature set to Gatsby.
Transform plain text into dynamic blogs and websites using the latest web technologies. A React.js static site generator.
Build sites like it's 1995. Files are translated into HTML pages at the
same position within the file system. Add a markdown file at /docs/index.md
and
it'll be converted to /docs/index.html
.
Supports Markdown, HTML, and React.js components out of the box. Easy to add support for additional files types.
Leverages [React Router's "nested component hierarchy"](http://rackt.github.io/react-router/#Router Overview) to make templating incredibly intuitive.
All templates, css, and content is hot reloadable.
Gatsby is very new. APIs will break. Functionality is missing. It's usable but if you plan on building with it, expect a rocky road for some time.
Contributions welcome!
npm install -g gatsby
gatsby new my-test-gatsby-site
This creates the
directory for your Gatsby project and adds the minimal files
needed.cd my-test-gatsby-site
gatsby serve
— Gatsby will compile your code using Webpack and
serve the site at localhost:8000If you're site falls closer to the site end of the app<---->site spectrum then yes.
Gatsby is an excellent fit for blogs, marketing sites, docs sites, etc. Proper web apps should probably remain as normal web apps (though I'd love to be proved wrong!).
npm install -g gatsby
gatsby new docs-site gh:gatsbyjs/gatsby-starter-documentation
cd docs-site
gatsby serve
config.toml
.
Change the siteTitle
key. The site's title should change shortly
after saving./pages/docs/getting-started/index.md
and edit it. Again any saved
changes should load without refreshing in the browser.getting-started
directory to some-additional-steps
. Then edit the markdown file
within the new directory. If you're familiar with other static site
generation software, you'll be familiar with the "frontmatter" at the
top of the file. Edit the title there + change the order to "5". Save
this. Ideally this new file would be hot reloaded like other changes
but I haven't figured out how to make this happen yet (help
appreciated here).
So to see your new page, restart gatsby serve
and then refresh your
browser.gatsby build
. The site is built to the /public
directory. Serve the site by going into the public directory and
typing python -m SimpleHTTPServer
The process is file --> Webpack loader --> React.js wrapper component --> static html page.
Gatsby leverages Webpack extensively. Webpack is a sophisticated module bundler that can turn any sort of file into a commonjs module. Webpack uses "Loaders" to convert a file into a module. These loaded modules are then wrapped inside a react.js component that's specific to a given file type. Gatsby then generates a static html page from this component.
Gatsby ships with default loaders and wrappers for HTML, Markdown, and JSX/CJSX but for most projects you'll want to write your own loaders and wrappers (very easy to do).
As an example of how this process works, let's walk quickly through converting a markdown file into an html page.
The default Gatsby markdown loader parses the markdown into HTML and uses Highlight.js to syntax highlight code blocks.
Our markdown file.
---
title: This is a title
---
# Hi friends.
This is a markdown file.
When loaded and required, the resulting javascript object looks like the following.
{
file: {
// Information about file on disk e.g. extension, directory path, etc.
},
data: {
title: "This is a title",
body: "<h1>Hi friends.</h1><p>This is a markdown file</p>"
}
}
Now Gatsby wraps the markdown file in this very simple React.js component.
module.exports = React.createClass({
displayName: "MarkdownWrapper",
render: function() {
post = @props.page.data
<div className="markdown">
<h1>{post.title}</h1>
<div dangerouslySetInnerHTML={{__html: post.body}}/>
</div>
}
})
/pages
— All pages go here. Everything is turned into a page except
files which start with an underscore_template
files — coming...FAQs
Blazing fast modern site generator for React
We found that gatsby demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 7 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.