Skip to content

feat: better blog sharing #307

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

Closed
wants to merge 10 commits into from
Closed

feat: better blog sharing #307

wants to merge 10 commits into from

Conversation

georgiee
Copy link
Member

@georgiee georgiee commented Dec 22, 2021

Preview: https://satellytescommain21751-gkfixblogsharing.gtsb.io/

  • Create the sharing cards with createFileNodeFromBuffer instead of writing into public
  • Updated our buildGatsbyCloudPreviewUrl as it used the wrong prefix
  • Create share images for blog from the teaser instead of using the card
  • Node set to v16 *engine and gatsby env

Blog with Image Preview:
Screen Shot 2021-12-22 at 10 21 39

Cards are still working:
Screen Shot 2021-12-22 at 10 21 46

@netlify
Copy link

netlify bot commented Dec 22, 2021

✔️ Deploy Preview for satellytes-website-storybook ready!

🔨 Explore the source changes: 45c1ca5

🔍 Inspect the deploy log: https://app.netlify.com/sites/satellytes-website-storybook/deploys/61c335142eeb360008444892

😎 Browse the preview: https://deploy-preview-307--satellytes-website-storybook.netlify.app

@georgiee georgiee changed the title chore: fix blog sharing feat: better blog sharing Dec 22, 2021
@gatsby-cloud
Copy link

gatsby-cloud bot commented Dec 22, 2021

Gatsby Cloud Build Report

🚩 Your build failed. See the build logs here

Errors

Page data from page-data.json for the failed page "/de/career/frontend-engineer/": {
  "componentChunkName": "component---src-templates-career-details-tsx",
  "path": "/de/career/frontend-engineer/",
  "result": {
    "data": {
      "syPersonioJob": {
        "socialCard": null,
        "id": "a7757a4a-2b4b-57f6-b3bb-b972359dadeb",
        "lang": "de",
        "jobId": 338504,
        "name": "Frontend Engineer (w/m/x)",
        "short": "Wir suchen eine:n Frontend Engineer (w/m/x) mit erster professioneller Erfahrung als Webentwickler:in. Werde Teil eines Teams, bei dem du von Experten den Umgang mit Frameworks & Testing ausbauen und verfeinern kannst.",
        "createdAt": "2021-03-17T14:57:12+00:00",
        "slug": "/career/frontend-engineer/",
        "sections": [
          {
            "headline": "Was du tun wirst",
            "descriptionHtml": "
  • Entwicklung von Web-Applikationen (Angular oder React, manchmal Vue)
  • Entwicklung und Pflege von UI-Libraries (lokal oder verteilt)
  • Anbindung sowie Mocking von REST-basierten APIs
  • E2E-Testing mit Cypress
", "description": "Entwicklung von Web-Applikationen (Angular oder React, manchmal Vue)Entwicklung und Pflege von UI-Libraries (lokal oder verteilt)Anbindung sowie Mocking von REST-basierten APIsE2E-Testing mit Cypress" }, { "headline": "Was du mitbringst", "descriptionHtml": "
  • Du solltest mindestens zwei Jahre professionell als Webentwickler:in mit Frameworks wie React oder Angular gearbeitet haben.
  • Erfahrung im Umgang mit Testing Tools wie Jest & Cypress. Testing ist bei uns Teil der täglichen Arbeit.

@georgiee georgiee force-pushed the gk-fix-blog-sharing branch 3 times, most recently from 91ad100 to 7e2546b Compare December 22, 2021 12:43
@georgiee georgiee force-pushed the gk-fix-blog-sharing branch from 7e2546b to 5f79cd5 Compare December 22, 2021 13:55
@georgiee
Copy link
Member Author

back in game, everything works, including the breaking incremental builds.
This was because of the gatsby foreign key issue. We relied on the deprecated way (see https://www.gatsbyjs.com/docs/reference/release-notes/migrating-from-v3-to-v4/#___node-convention-is-deprecated) where an postfix of ___NODE signals the foreign key relationship. This causes problems in gatsby 4 it seems. After I switched to the modern way through createSchemaCustomization (see https://www.gatsbyjs.com/docs/how-to/plugins-and-themes/creating-a-source-plugin/#create-foreign-key-relationships-between-data) it works flawless 🙌

We now have correct cards and the blog shows the correct teaser image instead of the card (we still create the card so we don't have to filter the blog md documents, okay for now).

I used two ways to define the new schema. The syntactic sugar from gatsby (or is it graphql?) with @link:

socialCard: File @link(from: "fields.socialCard")

and the more explicit way of using an resolver:

      fields: {
        socialCard: {
          type: 'File',
          resolve: (source, args, context, info) => {
            return context.nodeModel.getNodeById({
              id: source.fields.socialCard,
              type: 'File',
            });
          },
        },
      },

both have the same effect that they create a field socialCard in the root of the node with the actual node that is referenced through fields.socialCard. I wonder if we could put the actual card creation in the resolve or if it's better to leave it separated as is🧐

There the proof it's working now 🙌

Screen Shot 2021-12-22 at 15 05 10

Screen Shot 2021-12-22 at 15 05 02

Screen Shot 2021-12-22 at 15 03 12

@georgiee georgiee marked this pull request as ready for review December 22, 2021 14:16
@georgiee georgiee requested a review from a team as a code owner December 22, 2021 14:16
Copy link
Member Author

@georgiee georgiee left a comment

Choose a reason for hiding this comment

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

some comments

@@ -17,7 +17,7 @@ jobs:
uses: actions/checkout@master
- uses: actions/setup-node@v2-beta
with:
node-version: '14'
node-version: '16'
Copy link
Member Author

Choose a reason for hiding this comment

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

While debugging, but I think this not wrong at all to bump for our project.

Comment on lines +6 to +15
const GATSBY_SITE_PREFIX = process.env.GATSBY_SITE_PREFIX || '';
const BRANCH_PREVIEW_URL = buildGatsbyCloudPreviewUrl({
prefix: GATSBY_SITE_PREFIX,
branch: process.env.BRANCH,
});

// either use a branch preview url if any
const BASE_URL =
BRANCH_PREVIEW_URL || process.env.GATBSY_BASE_UR || 'http://localhost:8000';

Copy link
Member Author

Choose a reason for hiding this comment

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

I untangled the fallback to focus on the branch url creation. I think that's easier to follow now

Comment on lines +35 to +81
exports.createSchemaCustomization = ({ actions, schema }) => {
const { createTypes } = actions;

const typeDefs = [
/**
* Variant 1: use `@link` and link the given field
* You can't replace fields.socialCard hence the creation of the node in the parent
*/
`type MarkdownRemark implements Node {
socialCard: File @link(from: "fields.socialCard")
}`,
/**
* Variant 2: use `buildObjectType` and resolve the
* foreign key relation through a resolver. The `@link` above
* is basically syntactic sugar for this. I want to show both variants
* here for education purposes as it was a long bumpy road.
*
* Before ew used the deprecated fielname___NODE way of adding
* the FK relationship which fails for incremental builds in gatsby 4
*
*
* Deprecated Note:
* https://www.gatsbyjs.com/docs/reference/release-notes/migrating-from-v3-to-v4/#___node-convention-is-deprecated
*
* How to "schema additions"
* https://www.gatsbyjs.com/docs/how-to/plugins-and-themes/creating-a-source-plugin/#create-foreign-key-relationships-between-data
*/
schema.buildObjectType({
name: 'SyPersonioJob',
fields: {
socialCard: {
type: 'File',
resolve: (source, args, context, info) => {
return context.nodeModel.getNodeById({
id: source.fields.socialCard,
type: 'File',
});
},
},
},
interfaces: ['Node'],
}),
];

createTypes(typeDefs);
};

Copy link
Member Author

Choose a reason for hiding this comment

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

yeah that solved my hour long trouble getting the file properly connected to the career and blog posts. It always worked on a fresh build but incremental builds failed. Probably because the incremental build hat some issues restoring through the implicit fk ___NODE assignment we used before. much better because it's explicit now.

Comment on lines +62 to +77
schema.buildObjectType({
name: 'SyPersonioJob',
fields: {
socialCard: {
type: 'File',
resolve: (source, args, context, info) => {
return context.nodeModel.getNodeById({
id: source.fields.socialCard,
type: 'File',
});
},
},
},
interfaces: ['Node'],
}),
];
Copy link
Member Author

Choose a reason for hiding this comment

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

This could be the same one liner as above for MarkdownRemark, but I wanted to keep it for now to demonstrate the alternative because I wonder if I should resolve and generated the card in here. Fine to keep it around for now or better put it into an issue?

value: publicUrl,
});
const buffer = await generateCardToBuffer({ title });
const fileNode = await createFileNodeFromBuffer({
Copy link
Member Author

Choose a reason for hiding this comment

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

finally I had time to create the file from buffer to create a proper file node instead of working around gatsby and output the file directly in static. That approach was never wrong but the 500 errors (which were caused by plain wrong urls we generated) brought me on this so. I like this much more. It's more complicated but less implicit.

});
const buffer = await generateCardToBuffer({ title });
const fileNode = await createFileNodeFromBuffer({
name: 'social-card',
Copy link
Member Author

Choose a reason for hiding this comment

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

extension is inferred from the buffer where I set jpg.

Comment on lines -58 to +41
createPreviewJob({ node, _, actions });
return createPreviewCard(node.name, { node, ...rest });
}

if (node.internal.type === 'MarkdownRemark') {
createPreviewMarkdown({ node, _, actions });
return createPreviewCard(node.frontmatter.title, { node, ...rest });
Copy link
Member Author

Choose a reason for hiding this comment

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

I only need to pass in the actual string to generate. Before it was two methods while it's actually a single one 👍

site.siteMetadata.siteUrl + DEFAULT_META_IMAGE_URL_PATH;
const metaImageUrl = imageUrl ?? defaultImageUrl;

const metaImageUrl =
Copy link
Member Author

Choose a reason for hiding this comment

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

every image we pass in need to be path. never an url as seo should resolve that. which we do now.

Comment on lines +23 to +24
const shareImagePath =
markdown.frontmatter.shareImage.childImageSharp.fixed.src;
Copy link
Member Author

Choose a reason for hiding this comment

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

we switch from card to the image for blog posts here. The shareImage is just another variant of the teaser cropped to the expected format.

@@ -9,7 +9,7 @@
"url": "[email protected]:satellytes/satellytes.com.git"
},
"engines": {
"node": ">= 14.0.0",
"node": ">= 16.0.0",
Copy link
Member Author

Choose a reason for hiding this comment

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

this will cause yarn to complain when a local node version is lower. let's see who will complain 😁

@georgiee
Copy link
Member Author

damn. this is nuts. is this gatsby 4 killing us?
Screen Shot 2021-12-22 at 15 33 30

@georgiee georgiee marked this pull request as draft December 22, 2021 15:45
@georgiee
Copy link
Member Author

back to draft. this might be best handled in isolation while I can bring the parts that work through #308

@georgiee
Copy link
Member Author

closed in favor of #309

@georgiee georgiee closed this Dec 23, 2021
@feedm3 feedm3 deleted the gk-fix-blog-sharing branch March 7, 2022 10:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant