diceline-chartmagnifierquestion-marktwitter-whiteTwitter_Logo_Blue

Today I Learned

Best Practices for URLs When Concatenating Domains with Paths

When working with URLs, it's important to follow best practices to ensure proper formatting and avoid common issues. Here are two key guidelines to keep in mind:

  1. Concatenating Domains with Paths: When concatenating domain names with URL paths, it is recommended to follow this approach:

    • Ensure that the domain name does not have a trailing slash. For example, "example.com" is preferred over "example.com/".
    • Start the concatenated path with a slash. For example, "/api/endpoint" is preferred over "api/endpoint" or "//api/endpoint".

    Following this convention helps maintain consistency and adhere to the URL structure guidelines defined by the HTTP specification. By having the domain name without a trailing slash and starting the path with a slash, you ensure proper URL formatting and avoid potential issues with duplicate slashes or incorrect path resolution.

  2. Removing Trailing Slashes: To remove the trailing slash from a string in JavaScript, you can use a simple function. Here's an example:

    function removeTrailingSlash(str) {
      if (str.endsWith('/')) {
        return str.slice(0, -1);
      }
      return str;
    }
    

    This function checks if the last character of the string is a slash and removes it if present. You can use it to remove the trailing slash from a domain name or any other string.

    Following these guidelines helps ensure URL consistency, avoid common formatting issues, and maintain clean and well-formed URLs.

Remember, adhering to best practices when working with URLs promotes standardization, readability, and compatibility, leading to better overall web development practices.

Best Practices for URLs When Concatenating Domains with Paths

When working with URLs, it's important to follow best practices to ensure proper formatting and avoid common issues. Here are two key guidelines to keep in mind:

  1. Concatenating Domains with Paths: When concatenating domain names with URL paths, it is recommended to follow this approach:

    • Ensure that the domain name does not have a trailing slash. For example, "example.com" is preferred over "example.com/".
    • Start the concatenated path with a slash. For example, "/api/endpoint" is preferred over "api/endpoint" or "//api/endpoint".

    Following this convention helps maintain consistency and adhere to the URL structure guidelines defined by the HTTP specification. By having the domain name without a trailing slash and starting the path with a slash, you ensure proper URL formatting and avoid potential issues with duplicate slashes or incorrect path resolution.

  2. Removing Trailing Slashes: To remove the trailing slash from a string in JavaScript, you can use a simple function. Here's an example:

    function removeTrailingSlash(str) {
      if (str.endsWith('/')) {
        return str.slice(0, -1);
      }
      return str;
    }
    

    This function checks if the last character of the string is a slash and removes it if present. You can use it to remove the trailing slash from a domain name or any other string.

    Following these guidelines helps ensure URL consistency, avoid common formatting issues, and maintain clean and well-formed URLs.

Remember, adhering to best practices when working with URLs promotes standardization, readability, and compatibility, leading to better overall web development practices.

Best Practices for URLs When Concatenating Domains with Paths

When working with URLs, it's important to follow best practices to ensure proper formatting and avoid common issues. Here are two key guidelines to keep in mind:

  1. Concatenating Domains with Paths: When concatenating domain names with URL paths, it is recommended to follow this approach:

    • Ensure that the domain name does not have a trailing slash. For example, "example.com" is preferred over "example.com/".
    • Start the concatenated path with a slash. For example, "/api/endpoint" is preferred over "api/endpoint" or "//api/endpoint".

    Following this convention helps maintain consistency and adhere to the URL structure guidelines defined by the HTTP specification. By having the domain name without a trailing slash and starting the path with a slash, you ensure proper URL formatting and avoid potential issues with duplicate slashes or incorrect path resolution.

  2. Removing Trailing Slashes: To remove the trailing slash from a string in JavaScript, you can use a simple function. Here's an example:

    function removeTrailingSlash(str) {
      if (str.endsWith('/')) {
        return str.slice(0, -1);
      }
      return str;
    }
    

    This function checks if the last character of the string is a slash and removes it if present. You can use it to remove the trailing slash from a domain name or any other string.

    Following these guidelines helps ensure URL consistency, avoid common formatting issues, and maintain clean and well-formed URLs.

Remember, adhering to best practices when working with URLs promotes standardization, readability, and compatibility, leading to better overall web development practices.

Best Practices for URLs When Concatenating Domains with Paths

When working with URLs, it's important to follow best practices to ensure proper formatting and avoid common issues. Here are two key guidelines to keep in mind:

  1. Concatenating Domains with Paths: When concatenating domain names with URL paths, it is recommended to follow this approach:

    • Ensure that the domain name does not have a trailing slash. For example, "example.com" is preferred over "example.com/".
    • Start the concatenated path with a slash. For example, "/api/endpoint" is preferred over "api/endpoint" or "//api/endpoint".

    Following this convention helps maintain consistency and adhere to the URL structure guidelines defined by the HTTP specification. By having the domain name without a trailing slash and starting the path with a slash, you ensure proper URL formatting and avoid potential issues with duplicate slashes or incorrect path resolution.

  2. Removing Trailing Slashes: To remove the trailing slash from a string in JavaScript, you can use a simple function. Here's an example:

    function removeTrailingSlash(str) {
      if (str.endsWith('/')) {
        return str.slice(0, -1);
      }
      return str;
    }
    

    This function checks if the last character of the string is a slash and removes it if present. You can use it to remove the trailing slash from a domain name or any other string.

    Following these guidelines helps ensure URL consistency, avoid common formatting issues, and maintain clean and well-formed URLs.

Remember, adhering to best practices when working with URLs promotes standardization, readability, and compatibility, leading to better overall web development practices.

Best Practices for URLs When Concatenating Domains with Paths

When working with URLs, it's important to follow best practices to ensure proper formatting and avoid common issues. Here are two key guidelines to keep in mind:

  1. Concatenating Domains with Paths: When concatenating domain names with URL paths, it is recommended to follow this approach:

    • Ensure that the domain name does not have a trailing slash. For example, "example.com" is preferred over "example.com/".
    • Start the concatenated path with a slash. For example, "/api/endpoint" is preferred over "api/endpoint" or "//api/endpoint".

    Following this convention helps maintain consistency and adhere to the URL structure guidelines defined by the HTTP specification. By having the domain name without a trailing slash and starting the path with a slash, you ensure proper URL formatting and avoid potential issues with duplicate slashes or incorrect path resolution.

  2. Removing Trailing Slashes: To remove the trailing slash from a string in JavaScript, you can use a simple function. Here's an example:

    function removeTrailingSlash(str) {
      if (str.endsWith('/')) {
        return str.slice(0, -1);
      }
      return str;
    }
    

    This function checks if the last character of the string is a slash and removes it if present. You can use it to remove the trailing slash from a domain name or any other string.

    Following these guidelines helps ensure URL consistency, avoid common formatting issues, and maintain clean and well-formed URLs.

Remember, adhering to best practices when working with URLs promotes standardization, readability, and compatibility, leading to better overall web development practices.

Best Practices for URLs When Concatenating Domains with Paths

  1. Concatenating Domains with Paths:

    • Domain: Avoid trailing slashes (e.g., "example.com" over "example.com/").
    • Path: Start with a slash (e.g., "/api/endpoint" over "api/endpoint" or "//api/endpoint").
  2. Removing Trailing Slashes:

    • JavaScript Function:
    function removeTrailingSlash(str) {
      return str.endsWith('/') ? str.slice(0, -1) : str;
    }
    

Following these practices ensures proper URL formatting, avoids issues, and promotes clean and standardized URLs.

How To Change The Slug of A Custom Post Type in WordPress

To change the slug for a post type in WordPress, you can use the register_post_type() function. Within the arguments array for the function, you can set the rewrite parameter to an array with the new slug you want to use.

Here's an example code snippet:

function change_post_type_slug() {
    $args = array(
        'rewrite' => array( 'slug' => 'new-slug' ),
        // other post type arguments
    );
    register_post_type( 'your_post_type', $args );
}
add_action( 'init', 'change_post_type_slug' );

In this example, replace your_post_type with the name of the post type you want to change, and replace new-slug with the new slug you want to use.

Once you've added this code to your functions.php file, you'll need to go to Settings > Permalinks in the WordPress admin dashboard and click the "Save Changes" button to update your permalinks. This will ensure that your new slug is properly applied.

How to Configure Traefik to Use Existing TLS Certificates

  1. Create a new file tls.yml
tls:
  stores:
    default:
      defaultCertificate:
        certFile: /etc/traefik/certs/your-domain.dev/cert.pem
        keyFile: /etc/traefik/certs/your-domain.dev/privkey.pem
  1. Make sure you're copying this file in your Dockerfile

COPY .docker/traefik/conf.d/tls.yml /etc/traefik/tls.yml

  1. Mount the certs folder in your docker-compose.yml file
volumes:
  ...
	- .docker/traefik/certs:/etc/traefik/certs
  ...
  1. Update the traefik.yml config to support the file provider:
providers:
		...
    file:
      filename: "/etc/traefik/tls.yml
    ...

How To Keep Your Github Actions Workflows Private in an Open Source Repository

If you want to make your repository open source, but keep the workflows private, you can do the following:

  1. Create a separate repository for your workflows and make it private.
  2. In the open source repository, include only the necessary configuration files (e.g. .github/actions) and reference the private repository as a submodule.
  3. In the private repository, configure the GitHub Actions workflows as you normally would.
  4. When someone clones or forks the open source repository, the submodule reference to the private repository will not be included.

This way, you can keep your workflows private, while still making your repository open source.

Meteor.js powered React hook for retrieving articles with error and loading state handling

The custom hook created uses Meteor's useTracker hook and the subscribe method to subscribe to the article publication, as well as the ArticlesCollection to find the article with the matching _id (which is the same as the slug in this case).

const useArticle = (slug: string): any => {
    const [error, setError] = useState<Error | null>(null)

    return useTracker(() => {
        const sub = Meteor.subscribe('article', { _id: slug })
        if (sub.ready()) {
            const article = ArticlesCollection.findOne({ _id: slug })
            if (article) {
                return {
                    loading: false,
                    error: null,
                    article,
                }
            } else {
                setError(new Error(`Article with slug "${slug}" not found`))
                return {
                    loading: false,
                    error,
                    article: undefined,
                }
            }
        } else {
            return {
                loading: true,
                error: null,
                article: undefined,
            }
        }
    }, [slug, setError])
}

This hook can be used in a React component to display the article, its loading state and error:

const Article = ({ slug }: { slug: string }) => {
    const { loading, error, article } = useArticle(slug)

    if (loading) {
        return <div>Loading...</div>
    }

    if (error) {
        return <div>Error: {error.message}</div>
    }

    return (
        <div>
            <h1>{article.title}</h1>
            <p>{article.body}</p>
        </div>
    )
}

Eager Loading with Subqueries in Laravel Eloquent

Eager loading with subqueries allows you to load related models using a subquery instead of joining the tables, which can improve performance in certain situations. For example, if you have a User model and a Post model, you can count the number of posts created by each user in the last month using a subquery like this:

$users = User::withCount(['posts as post_count' => function ($query) {
    $query->select(DB::raw('count(*)'))
          ->where('created_at', '>', now()->subMonth());
}])->get();