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

Scheduled posts #26

Closed
cferdinandi opened this issue Jan 5, 2018 · 12 comments
Closed

Scheduled posts #26

cferdinandi opened this issue Jan 5, 2018 · 12 comments
Labels
enhancement needs-votes A feature request on the backlog that needs upvotes or downvotes. Remove this label when resolved.

Comments

@cferdinandi
Copy link

One of the big things that keeps me away from static site generators is the ability to schedule posts to go live at a future date/time.

I know it's a bit complicated, but I would a way to schedule posts in advance (even if it's just documentation on setting up the right continuous deployment script or something).

@chrisdmacrae
Copy link
Contributor

chrisdmacrae commented Apr 12, 2018

My thoughts on keeping this fairly generic:

  • Generate an index of future dated posts at every build
  • Use a free serverless fxn library (e.g, webtask) to take this index
  • This serverless fxn takes the nearest future-date, and schedules itself to run again at that time
  • It also triggers a rebuild of your CI/build script
  • Every time a rebuild happens this index is updated, so it self-maintains

Webtask even has a simple-to-setup web interface for managing fxns, so this could totally be implemented a plugin with front-to-back support.


The only part that isn't handled for the user is trigger their build process, because that would be different for everyone.

@zachleat zachleat added the needs-votes A feature request on the backlog that needs upvotes or downvotes. Remove this label when resolved. label May 3, 2018
@zachleat
Copy link
Member

zachleat commented May 3, 2018

This repository is now using lodash style issue management for enhancements (see https://twitter.com/samselikoff/status/991395669016436736)

This means enhancement issues will now be closed instead of leaving them open. The enhancement backlog can be found here: https://github.com/11ty/eleventy/issues?utf8=%E2%9C%93&q=label%3Aneeds-votes+sort%3Areactions-%2B1-desc+

@zachleat zachleat closed this as completed May 3, 2018
@cmaxw
Copy link

cmaxw commented Jun 17, 2019

+1

@Ryuno-Ki
Copy link
Contributor

@cmaxw I believe you need to use a GitHub emoji on the initial post to make your vote count.

@cferdinandi I was wondering, whether I could use a future date + collection to control the showing up of a file. But I believe that would still make it render (personally, I'm keeping my drafts outside of the directory used for eleventy to render …).

Will try some things out and let you know how it went.

@cmaxw
Copy link

cmaxw commented Jun 18, 2019

I got you... I figured it out... See https://github.com/cmaxw/devchat-eleventy/blob/4d59ef949e36f55b2b9cb1af84a809a7cfcefcc7/.eleventy.js#L56

Specifically, I create my own filters that filter on date...

@Ryuno-Ki
Copy link
Contributor

Bonus points if you move the left hand side of https://github.com/cmaxw/devchat-eleventy/blob/4d59ef949e36f55b2b9cb1af84a809a7cfcefcc7/.eleventy.js#L61 out of the filter to save some milliseconds (because you only need to have that information once for comparison). Line 57 is fine.

@cmaxw
Copy link

cmaxw commented Jun 19, 2019

@Ryuno-Ki The left hand side? or the right hand side?

Or in other words, the part where I pull the date on the podcast entry? or the part where I calculate the current date/time with moment?

I can see calculating the date with moment once and then using that value for the rest of the work.

@Ryuno-Ki
Copy link
Contributor

I can see calculating the date with moment once and then using that value for the rest of the work.

This.

Ah, but moment() is on the right hand side :-) Sorry. too hot here 😓

@zachleat
Copy link
Member

@robdodson
Copy link

robdodson commented Mar 6, 2021

I used to use this snippet from Remy but I think it only removes the post from collections—it doesn't set its permalink to false, so it will still produce a file with a live url. I don't know if the Google crawler will index the page (since nothing is linking to it) but folks can type the url in to view it.

I've been thinking of using a snippet like this instead. It doesn't do scheduled posts at the moment, though that would be easy to add.

module.exports = {
  eleventyComputed: {
    /**
     * Adds support for drafts.
     * If a page has `draft: true` in its YAML frontmatter then this snippet
     * will set its permalink to false and exclude it from collections.
     *
     * For dev builds we will always render the page.
     */
     permalink: data => {
      if (process.env.NODE_ENV === 'production' && data.draft) {
        return false;
      }

      return data.permalink;
    },
    eleventyExcludeFromCollections: data => {
      if (process.env.NODE_ENV === 'production' && data.draft) {
        return true;
      }

      return false;
    }
  }
}

@sarunw
Copy link

sarunw commented Mar 16, 2021

@robdodson, thanks for sharing this. This is exactly what I am looking for.

@cbracco
Copy link

cbracco commented Apr 3, 2021

@robdodson Thanks for this! I was able to get scheduled posts working using your computed data file (with the addition of checking dates) and automating daily builds with GitHub Actions.

path/to/posts/posts.11tydata.js

module.exports = {
    eleventyComputed: {
        /**
         * Handle drafts and scheduled posts
         * https://github.com/11ty/eleventy/issues/26#issuecomment-791993563
         *
         * For production mode, if a post has `draft: true` in its YAML
         * frontmatter or it has a future date, then this snippet will set
         * `permalink: false` and exclude it from collections. This prevents the
         * post from being built.
         *
         * The actual publishing (aka building) of scheduled posts is handled
         * by GitHub Actions’ cron feature, which runs daily at 00:00 UTC.
         *
         * For dev mode, always render all posts.
         */
        permalink: data => {
            if (process.env.NODE_ENV === 'production' && (data.draft || data.page.date >= new Date())) {
                return false;
            }
            return data.permalink;
        },
        eleventyExcludeFromCollections: data => {
            if (process.env.NODE_ENV === 'production' && (data.draft || data.page.date >= new Date())) {
                return true;
            }
            return false;
        }
    }
}

.github/workflows/build.yml

name: Build Eleventy

on:
  push:
    branches:
      - master
  schedule:
    - cron: '0 0 * * *' # every day at 00:00 UTC

jobs:
  build:
    runs-on: ubuntu-20.04

    strategy:
      matrix:
        node-version: [12.x]

    steps:
      - uses: actions/checkout@v2

      - name: Use Node.js ${{ matrix.node-version }}
        uses: actions/setup-node@v1
        with:
          node-version: ${{ matrix.node-version }}

      - name: Install dependencies & build
        run: |
          npm ci
          npm run build

      - name: Deploy
        uses: peaceiris/actions-gh-pages@v3
        with:
          publish_dir: ./public
          github_token: ${{ secrets.GITHUB_TOKEN }}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement needs-votes A feature request on the backlog that needs upvotes or downvotes. Remove this label when resolved.
Projects
None yet
Development

No branches or pull requests

8 participants